P3118 [USACO15JAN]Moovie Mooving G
P3118 [USACO15JAN]Moovie Mooving G
题目描述
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).
'
奶牛贝西想连续看L (1 <= L <= 100,000,000)分钟的电影,有 N (1 <= N <= 20) 部电影可供选择,每部电影会在一天的不同
时段放映。
贝西可以在一部电影播放过程中的任何时间进入或退出放映厅。但她不愿意重复看到一部电影,所以每部电影她最多看到
一次。她也不能在看一部电影的过程中,换到另一个正在播放相同电影的放映厅。
请帮贝西计算她能够做到从 0 到 L 分钟连续不断地观看电影,如果能,请计算她最少看几部电影就行了。'
输入
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
0 2 20 90
20 1 0
样例输出
3
状态压缩 dp + 二分。
一开始看错题了,以为要看够 \(L\) 分钟,结果写了半天才发现是要从 \(0\) 一直看到 \(L\)
看到 \(n\) 的范围那么小 , \(L\) 的范围那么大,我们可以考虑把 \(L\) 的值压入数组中。
设 $f[s] $ 表示看电影集合为 \(s\) 的时候,看电影能持续到多长时间。
转移时我们可以枚举这个状态所有能看的电影,就会有转移。
f[i] = max(f[i], a[j][t] + t[i]) (i&(1<<j-1) == 1)
\(t\) 是使 \(a[j][t]\) >= f [i ^ (1<<(j-1))] 的数,即播放时间大于不看这部电影的延续的最长时间。
因为下一个电影的合法开始时间越晚,答案肯定更优。
最后收集答案就枚举每个状态,看这个状态的延续时间是否大于 \(L\), 如果大于就把这个状态看电影的个数和答案取个 \(min\)
Code
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,k,ans = 2147483647;
int t[55],num[55],movie[55][1010],f[2097152];
inline int read()
{
int s = 0,w = 1; char ch = getchar();
while(ch < '0' || ch > '9'){if(ch == '-') w = -1; ch = getchar();}
while(ch >= '0' && ch <= '9'){s =s * 10 + ch - '0'; ch = getchar();}
return s * w;
}
int lower__bound(int now,int k)
{
int L = 1, R = num[now], res = -1;
while(L <= R)
{
int mid = (L + R)>>1;
if(movie[now][mid] <= k)
{
res = mid;
L = mid + 1;
}
else R = mid - 1;
}
return res;
}
int main()
{
n = read(); k = read();
for(int i = 1; i <= n; i++)
{
t[i] = read(); num[i] = read();
for(int j = 1; j <= num[i]; j++)
{
movie[i][j] = read();
}
}
for(int i = 1; i < (1<<n); i++)
{
for(int j = 1; j <= n; j++)
{
if(i & (1<<(j-1)))
{
int id = lower__bound(j,f[i ^ (1<<(j-1))]);//找第一个大于 k 的数
if(id != -1)
{
f[i] = max(f[i], movie[j][id] + t[j]);
}
}
}
}
for(int i = 1; i < (1<<n); i++)
{
if(f[i] >= k)
{
int cnt = 0, x = i;
while(x)
{
cnt += x&1;
x >>= 1;
}
ans = min(ans,cnt);
}
}
if(ans == 2147483647) printf("%d\n",-1);
else printf("%d\n",ans);
return 0;
}
P3118 [USACO15JAN]Moovie Mooving G的更多相关文章
- [USACO15JAN]Moovie Mooving G
[USACO15JAN]Moovie Mooving G 状压难题.不过也好理解. 首先我们根据题意: she does not want to ever visit the same movie t ...
- Luogu3118:[USACO15JAN]Moovie Mooving
题面 传送门 Sol 设\(f[S]\)表示看过的电影集合为\(S\),当前电影的最大结束时间 枚举电影和电影的开始时间转移 可以对开始时间\(sort\) 二分一下转移即可 # include &l ...
- [USACO15JAN]电影移动Moovie Mooving
[USACO15JAN]电影移动Moovie Mooving 时间限制: 2 Sec 内存限制: 128 MB 题目描述 Bessie is out at the movies. Being mis ...
- P3119 [USACO15JAN]Grass Cownoisseur G
P3119 [USACO15JAN]Grass Cownoisseur G tarjan缩点+分层图上跑 spfa最长路 约翰有 \(n\) 块草场,编号 \(1\) 到 \(n\),这些草场由若干条 ...
- [bzoj3886] [USACO15JAN]电影移动Moovie Mooving
题目链接 状压\(dp\). 注意到\(n\leq 20\)且每个只能用一次,所以很显然可以压缩每部电影看过没,记\(f[sta]\)为状态为\(sta\)时最多可以看多久. 转移时先枚举状态,然后枚 ...
- BZOJ3886 : [Usaco2015 Jan]Moovie Mooving
f[i]表示用i集合内的电影可以达到的最长时间 f[i]向f[i|(1<<j)]更新,此时的时间为第j部电影在f[i]前的最晚上映时间 先排序一遍离散化后用前缀最大值解决 时间复杂度$O( ...
- 【bzoj3886】[Usaco2015 Jan]Moovie Mooving 状态压缩dp+二分
题目描述 Bessie is out at the movies. Being mischievous as always, she has decided to hide from Farmer J ...
- [Usaco2015 Jan]Moovie Mooving
Description Bessie is out at the movies. Being mischievous as always, she has decided to hide from F ...
- DP测试总结
T1:三取方格数 题目描述 设有N*N的方格图,我们将其中的某些方格填入正整数,而其他的方格中放入0.某人从图得左上角出发,可以向下走,也可以向右走,直到到达右下角.在走过的路上,他取走了方格中的数. ...
随机推荐
- Python数据清洗:提取爬虫文本中的电话号码
步骤索引 效果展示 注意事项 代码 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的知识. ...
- oeasy教您玩转linux010202软件包管理apt
顾一下 上一部分我们都讲了什么?
- gson 处理null
1.定义null处理类 class StringConverter : JsonSerializer<String?>, JsonDeserializer<String?> { ...
- 初级知识六——C#事件通知系统实现(观察者模式运用)
观察者模式,绝对是游戏中十分重要的一种模式,运用这种模式,可以让游戏模块间的通信变得简单,耦合度也会大大降低,下面讲解如何利用C#实现事件通知系统. 补充,首先说下这个系统的实现原理,不然一头扎进去就 ...
- java初探(1)之登录总结
登录总结 前几章总结了登录各个步骤中遇到的问题,现在完成的做一个登录的案例,其难点不在于实现功能,而在于抽象各种功能模块,提高复用性,较低耦合度. 前端页面: 对于前端页面来说,不是后端程序员要考虑的 ...
- 条件竞争(race condition)
条件竞争漏洞是一种服务器端的漏洞,由于服务器端在处理不同用户的请求时是并发进行的,因此,如果并发处理不当或相关操作逻辑顺序设计的不合理时,将会导致此类问题的发生. 参考了一些资料,发现一个比较能说明问 ...
- Java网络通信 —— 序列化问题
Java序列化的目的主要有两个: 1.网络传输 2.对象持久化 当选行远程跨迸程服务调用时,需要把被传输的Java对象编码为字节数组或者ByteBuffer对象.而当远程服务读取到ByteBuffer ...
- 网易云uwp
起因 昨天晚上折腾Ubuntu 莫名其妙任务栏的网易云音乐图标消失了,今早才发现原来是更新了. but,这个更新真的是让人一言难尽 upw更新一下直接变成了桌面版? 折腾 重新装回老版uwp 网易云U ...
- python基础:网络编程
一.网络编程 简而言之,就是通过代码打开一个url,获得返回结果并做处理.通常所说的python爬虫,就属于网络编程 二.urllib模块进行网络编程 这个方法很繁琐,不建议使用.了解 示例1: 获取 ...
- 服务器搭建远程docker深度学习环境
服务器搭建远程docker深度学习环境 本文大部分内容参考知乎文章 Docker+PyCharm快速搭建机器学习开发环境 搭建过程中出现ssh连接问题可以查看最后的注意事项 Docker Docker ...