首页 > 其他分享 >NC24158 [USACO 2015 Jan G]Moovie Mooving

NC24158 [USACO 2015 Jan G]Moovie Mooving

时间:2022-09-04 02:00:07浏览次数:81  
标签:20 int movie USACO Jan Moovie time dp first

题目链接

题目

题目描述

Bessie is out at the movies. Being mischievous as always, she has decided to hide from Farmer John for L (1 <= L <= 100,000,000) minutes, during which time she wants to watch movies continuously. She has N (1 <= N <= 20) movies to choose from, each of which has a certain duration and a set of showtimes during the day. Bessie may enter and exit a movie at any time during one if its showtimes, but she does not want to ever visit the same movie twice, and she cannot switch to another showtime of the same movie that overlaps the current showtime. Help Bessie by determining if it is possible for her to achieve her goal of watching movies continuously from time 0 through time L. If it is, determine the minimum number of movies she needs to see to achieve this goal (Bessie gets confused with plot lines if she watches too many movies).

输入描述

The first line of input contains N and L.
The next N lines each describe a movie. They begin with its integer duration, D (1 <= D <= L) and the number of showtimes, C (1 <= C <= 1000). The remaining C integers on the same line are each in the range 0..L, and give the starting time of one of the showings of the movie. Showtimes are distinct, in the range 0..L, and given in increasing order.

输出描述

A single integer indicating the minimum number of movies that Bessie needs to see to achieve her goal. If this is impossible output -1 instead.

示例1

输入

4 100
50 3 15 30 55
40 2 0 65
30 2 20 90
20 1 0

输出

3

备注

Bessie should attend the first showing of the fourth movie from time 0 to time 20. Then she watches the first showing of the first movie from time 20 to time 65. Finally she watches the last showing of the second movie from time 65 to time 100.

题解

知识点:状压dp,二分。

TSP问题的变种,变化在于每个点都有不同条件以及对应的不同贡献,选择一个作为下一个点时,需要选择这个点的最优解贡献,可以在排序的情况下二分查找。

这道题需要选择一个电影后,查找这个电影合法且最佳播放时间,如果不存在一个合法的就不能选这个点。

时间复杂度 \(O(2^n\sum \log c_i)\)

空间复杂度 \(O(2^n + \sum c_i)\)

代码

#include <bits/stdc++.h>

using namespace std;

int d[27], c[27][1007];
int dp[(1 << 20) + 7];

int main() {
    std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int N, L;
    cin >> N >> L;
    for (int i = 1;i <= N;i++) {
        cin >> d[i];
        cin >> c[i][0];
        for (int j = 1;j <= c[i][0];j++)
            cin >> c[i][j];
    }
    memset(dp, -0x3f, sizeof(dp));
    dp[0] = 0;
    int ans = ~(1 << 31);
    for (int i = 0;i < (1 << N);i++) {
        for (int j = 1;j <= N;j++) {
            if (!(i & (1 << (j - 1))) || dp[i ^ (1 << (j - 1))] < 0) continue;///实际上不用判断上一个状态存不存在,因为如果不存在导致这一次从0开始加了,那一定存在一种更小的方案,所以也不影响答案
            int k = upper_bound(c[j] + 1, c[j] + c[j][0] + 1, dp[i ^ (1 << (j - 1))]) - c[j] - 1;///持续时间固定,找到开始时间最接近的
            if (k) dp[i] = max(dp[i], c[j][k] + d[j]);///存在k直接选,则开始时间+持续时间和已有最大值比较,如果小了,选了也不影响最终答案
        }
        if (dp[i] >= L) ans = min(ans, __builtin_popcount(i));
    }
    cout << (ans > N ? -1 : ans) << '\n';
    return 0;
}

标签:20,int,movie,USACO,Jan,Moovie,time,dp,first
From: https://www.cnblogs.com/BlankYang/p/16654147.html

相关文章

  • Django基础介绍二
    数据的查,改,删先讲数据库中的数据全部展示到前端然后给每一个数据两个按钮一个编辑一个删除查看defuserlist(request):#查询出用户表里面所有的数据#方式1#data......
  • Django CBV源码执行流程
         ......
  • django中操作mysql数据库
    1.准备工作(django连接数据库)1.本机电脑下载好mysql数据库2.打开django,修改setting.py中的DATABASES配置项DATABASES={'default':{'ENGINE':'django.d......
  • django-celery-beat 获取下一次执行时间
    前言因为业务需要获取下一次执行时间在前端展示,查阅百度,谷歌都没能找到实现方式。通过官方文档https://django-celery-beat.readthedocs.io/en/latest/reference/django-c......
  • django框架-04
    目录路由层网页伪静态视图层视图函数返回值django序列化方法form表单操作FBV与CBVFBVCBV模板层模板语法传值模板语法过滤器模板语法符号模板语法标签自定义相关功能模板继......
  • Django数据传递与模板语法
    Django数据传递与模板语法视图函数返回值1.视图函数的返回值其实本质上返回的都是HttpResponse对象,HttpResponse其实是一个类,我们最常使用的render和redirect都是这个类......
  • 【Django】 第04回 视图层与模板层
    目录1.网页伪静态2.视图层2.1视图函数的返回值问题2.2视图函数返回json格式数据2.3from表单携带文件数据2.4FEV与CBV2.5CBV源码分析3.模板层3.1模板语法传值3.2......
  • P2858 [USACO06FEB]Treats for the Cows G/S 题解
    [USACO06FEB]TreatsfortheCowsG/S[USACO06FEB]TreatsfortheCowsG/S题目描述FJhaspurchasedN(1<=N<=2000)yummytreatsforthecowswhogetmoneyfo......
  • django之视图层与模板层
    一、伪静态网页'''其实就是如果一个网页如果是一个静态网页的话那么浏览器搜索会更容易搜索的到而如果一个动态网页想要让浏览器更容易搜索到的话可以在路由匹配的时......
  • django_响应对象
    Django_响应对象响应对象响应对象有三种形式:HttpResponse()render()Redirect()(1)HttpResponse()django服务器接收到客户端发来的请求之后,会将提交上来的数据封装成......