题目描述

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).
PoPoQQQ要在电影院里呆L分钟,这段时间他要看小型电影度过。电影一共N部,每部都播放于若干段可能重叠的区间,PoPoQQQ决不会看同一部电影两次。现在问他要看最少几部电影才能度过这段时间? 注:必须看电影才能在电影院里呆着,同时一场电影可以在其播放区间内任意时间入场出场。

输入

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.

样例输入

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

样例输出

3


题解

状态压缩dp

设f[i]为状态i时最多能持续的时间。

那么就有f[i] = max(f[i] , a[j][k] + t[j]) (i&(1<<(j-1))==1,k为第一个使得a[j][k]>=f[i ^ (1 << (j - 1))]的数)。

于是二分查找即可,时间复杂度O(2^n*n*logd),看似很大,而亲测可过。

#include <cstdio>
#include <algorithm>
using namespace std;
int f[1050000] , t[21] , d[21] , a[21][1001] , num[1050000];
int search(int p , int x)
{
int l = 1 , r = d[p] , mid , ans = -1;
while(l <= r)
{
mid = (l + r) >> 1;
if(a[p][mid] <= x)
ans = mid , l = mid + 1;
else r = mid - 1;
}
return ans;
}
int main()
{
int n , m , i , j , k , ans = 0x7fffffff;
scanf("%d%d" , &n , &m);
for(i = 1 ; i <= n ; i ++ )
{
scanf("%d%d" , &t[i] , &d[i]);
for(j = 1 ; j <= d[i] ; j ++ )
scanf("%d" , &a[i][j]);
}
for(i = 1 ; i < 1 << n ; i ++ )
{
for(j = 1 ; j <= n ; j ++ )
{
if(i & (1 << (j - 1)))
{
k = search(j , f[i ^ (1 << (j - 1))]);
if(k != -1)
f[i] = max(f[i] , a[j][k] + t[j]);
}
}
}
for(i = 1 ; i < 1 << n ; i ++ )
{
num[i] = num[i - (i & (-i))] + 1;
if(f[i] >= m)
ans = min(ans , num[i]);
}
printf("%d\n" , ans == 0x7fffffff ? -1 : ans);
return 0;
}

【bzoj3886】[Usaco2015 Jan]Moovie Mooving 状态压缩dp+二分的更多相关文章

  1. BZOJ3886 : [Usaco2015 Jan]Moovie Mooving

    f[i]表示用i集合内的电影可以达到的最长时间 f[i]向f[i|(1<<j)]更新,此时的时间为第j部电影在f[i]前的最晚上映时间 先排序一遍离散化后用前缀最大值解决 时间复杂度$O( ...

  2. BFS+状态压缩DP+二分枚举+TSP

    http://acm.hdu.edu.cn/showproblem.php?pid=3681 Prison Break Time Limit: 5000/2000 MS (Java/Others)   ...

  3. 【bzoj3312】[Usaco2013 Nov]No Change 状态压缩dp+二分

    题目描述 Farmer John is at the market to purchase supplies for his farm. He has in his pocket K coins (1 ...

  4. [Usaco2015 Jan]Moovie Mooving

    Description Bessie is out at the movies. Being mischievous as always, she has decided to hide from F ...

  5. hoj2662 状态压缩dp

    Pieces Assignment My Tags   (Edit)   Source : zhouguyue   Time limit : 1 sec   Memory limit : 64 M S ...

  6. POJ 3254 Corn Fields(状态压缩DP)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4739   Accepted: 2506 Descr ...

  7. [知识点]状态压缩DP

    // 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...

  8. HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP

    题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...

  9. DP大作战—状态压缩dp

    题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...

随机推荐

  1. 20145226夏艺华《网络对抗》第一次实验拓展:shellcode注入+return-to-libc

    20145226夏艺华<网络对抗>第一次实验拓展:shellcode注入+return-to-libc shellcode注入实践 编写shellcode 编写shellcode已经在之前 ...

  2. 北京Uber优步司机奖励政策(3月14日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  3. dubbo之注册管理中心

    一.在dubbo的框架中注册中心是必要的一个环节,这个也是分布式部署的一个必要环节.在dubbo的架构基本图中可以看出,基本上所有的服务都是通过注册中心进行注册,然后在通过注册中心,暴露出接口来. 二 ...

  4. EDM站点

    设计邮件模版 http://templates.mailchimp.com/

  5. 鸡啄米:C++编程之十三学习之类与对象,类的声明,成员的访问控制

    1. 本次学习鸡啄米课程第13篇,把比较重要的学习记录下来,以敦促自己更好的学习.推荐他们的网址学习:http://www.jizhuomi.com/school/c/97.html 2. 在面向过程 ...

  6. 「国庆训练」Bomb(HDU-5934)

    题意 给定\(n\)个炸弹,每个炸弹的坐标与代价与影响范围给定,炸弹会引爆影响范围内其他所有炸弹.求引爆所有炸弹的最小代价. 分析 先做\(n^2\)的循环,然后建图,对\(i\)能引爆\(j\)建边 ...

  7. CentOS安装JMeter

    mkdir /usr/local/jmeter 新建jmeter目录 cd /usr/local/jmeter 进入jmeter目录 wget https://archive.apache.org/d ...

  8. webservice调用天气

    class WebServiceHelper { /// <summary> /// 动态调用WebService /// </summary> /// <param n ...

  9. JSP页面无法使用EL导致"java.sql.SQLException: No suitable driver found for ${snapshot}"的问题

    使用JSTL来连接mysql,这个问题折腾了半天,老以为是Mysql驱动的问题,还好最后偶然发现了是EL表达式识别不了,报错: javax.servlet.ServletException: java ...

  10. ArrayList与LinkedList的普通for循环遍历

    对于大部分Java程序员朋友们来说,可能平时使用得最多的List就是ArrayList,对于ArrayList的遍历,一般用如下写法: public static void main(String[] ...