题目链接

题目

题目描述

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;
}

NC24158 [USACO 2015 Jan G]Moovie Mooving的更多相关文章

  1. B【USACO 2015 Jan Gold】牧草鉴赏家

    时间限制 : 10000 MS   空间限制 : 65536 KB 问题描述 约翰有n块草场,编号1到n,这些草场由若干条单行道相连.奶牛贝西是美味牧草的鉴赏家,她想到达尽可能多的草场去品尝牧草. 贝 ...

  2. P3118 [USACO15JAN]Moovie Mooving G

    P3118 [USACO15JAN]Moovie Mooving G Link 题目描述 Bessie is out at the movies. Being mischievous as alway ...

  3. [USACO15JAN]Moovie Mooving G

    [USACO15JAN]Moovie Mooving G 状压难题.不过也好理解. 首先我们根据题意: she does not want to ever visit the same movie t ...

  4. Usaco 2019 Jan Platinum

    Usaco 2019 Jan Platinum 要不是昨天老师给我们考了这套题,我都不知道usaco还有铂金这么一级. 插播一则新闻:杨神坚持认为铂金比黄金简单,原因竟是:铜 汞 银 铂 金(金属活动 ...

  5. 【题解】晋升者计数 Promotion Counting [USACO 17 JAN] [P3605]

    [题解]晋升者计数 Promotion Counting [USACO 17 JAN] [P3605] 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训.!牛是可怕的管理者! [题目描 ...

  6. USACO翻译:USACO 2012 JAN三题(2)

    USACO 2012 JAN(题目二) 一.题目概览 中文题目名称 叠干草 分干草 奶牛联盟 英文题目名称 stacking baleshare cowrun 可执行文件名 stacking bale ...

  7. USACO翻译:USACO 2012 JAN三题(1)

    USACO 2012 JAN(题目一) 一.题目概览 中文题目名称 礼物 配送路线 游戏组合技 英文题目名称 gifts delivery combos 可执行文件名 gifts delivery c ...

  8. USACO翻译:USACO 2013 JAN三题(1)

    USACO 2013 JAN 一.题目概览 中文题目名称 镜子 栅栏油漆 奶牛排队 英文题目名称 mirrors paint lineup 可执行文件名 mirrors paint lineup 输入 ...

  9. USACO翻译:USACO 2014 JAN三题(2)

    USACO 2014 JAN 一.题目概览 中文题目名称 队伍平衡 滑雪录像 滑雪场建设 英文题目名称 bteams recording skicourse 可执行文件名 bteams recordi ...

  10. USACO翻译:USACO 2014 JAN三题(1)

    USACO 2014 JAN 一.题目概览 中文题目名称 滑雪场设计 滑雪降速 滑雪场评级 英文题目名称 skidesign slowdown skilevel 可执行文件名 skidesign sl ...

随机推荐

  1. 电机控制和Linux驱动开发哪个方向更好呢?

    电机控制和Linux驱动开发哪个方向更好呢? 先说结论:任何一个领域,就像世间的五行,阴阳结合,虚实结合,利弊结合.对于哪个更好,不能一概而论,最重要的是要搞清楚,你更适合哪个? 1.共鸣 当我看到这 ...

  2. MongoDB 部署分片集群

    部署配置服务器:configsvr 先生成.conf文件 mkdir -p /data/mongodb/configsvr vim /data/mongodb/configsvr/configsvr. ...

  3. [转帖]【sql server安全】sql server连接加密,sql server SSL加密连接

    https://www.cnblogs.com/gered/p/13595098.html#_label1_0 MSSQL - 最佳实践 - 使用SSL加密连接 回到顶部 author: 风移 回到顶 ...

  4. [转帖]故障分析 | 让top命令直接显示Java线程名?-- 解析OpenJDK的一个bug修复

    https://zhuanlan.zhihu.com/p/413136873 作者:阎虎青DBLE 开源项目负责人,负责分布式数据库中间件研发工作:持续专注于数据库方面的技术,始终在一线从事开发:对数 ...

  5. [转帖]TiDB 数据库统计表的大小方法

    简介:TiDB统计表的大小,列出了一些方法: 1.第一种的统计方式: 基于统计表 METRICS_SCHEMA.store_size_amplification 要预估 TiDB 中一张表的大小,你可 ...

  6. 银河麒麟安装LLDB的方法以及调试 dump 文件 (未完成)

    今天同事要进行 lldb进行调试dotnet的bug 本来在x86 上面进行相应的处理 但是发现报错. 没办法 正好有一台借来的arm服务器就搞了一下. 简单记录一下安装方法 1. 安装 apt的so ...

  7. c#通过表达式树优雅的实现分组取TopN笔记

    需要引入nuget包来实现ef.functions调用row_number Thinktecture.EntityFrameworkCore.SqlServer 调用方式: //顺排 context. ...

  8. go中bufio使用小结

    bufio 前言 例子 bufio 源码解析 Reader对象 实例化 ReadSlice ReadString ReadLine Peek Scanner Give me more data Err ...

  9. 【一】飞桨paddle【GPU、CPU】安装以及环境配置+python入门教学

    相关文章: [一]飞桨paddle[GPU.CPU]安装以及环境配置+python入门教学 [二]-Parl基础命令 [三]-Notebook.&pdb.ipdb 调试 [四]-强化学习入门简 ...

  10. 强化学习调参技巧一: DDPG算法训练动作选择边界值_分析解决

    1.原因: 选择动作值只在-1 1之间取值 actor网络输出用tanh,将动作规范在[-1,1],然后线性变换到具体的动作范围.其次,tanh激活区是有范围的,你的预激活变量(输入tanh的)范围太 ...