NC13885 Music Problem
题目
题目描述
Listening to the music is relax, but for obsessive(强迫症), it may be unbearable.
HH is an obsessive, he only start to listen to music at 12:00:00, and he will never stop unless the song he is listening ends at integral points (both minute and second are 0 ), that is, he can stop listen at 13:00:00 or 14:00:00,but he can't stop at 13:01:03 or 13:01:00, since 13:01:03 and 13:01:00 are not an integer hour time.
Now give you the length of some songs, tell HH whether it's possible to choose some songs so he can stop listen at an integral point, or tell him it's impossible.
Every song can be chosen at most once.
输入描述
The first line contains an positive integer , represents there are test cases.
For each test case:
The first line contains an integer , indicating there are songs.
The second line contains integers , the ith integer indicates the ith song lasts seconds.
输出描述
For each test case, output one line "YES" (without quotes) if HH is possible to stop listen at an integral point, and "NO" (without quotes) otherwise.
示例1
输入
3 3 2000 1000 3000 3 2000 3000 1600 2 5400 1800
输出
NO YES YES
说明
In the first example it's impossible to stop at an integral point.
In the second example if we choose the first and the third songs, they cost 3600 seconds in total, so HH can stop at 13:00:00
In the third example if we choose the first and the second songs, they cost 7200 seconds in total, so HH can stop at 14:00:00
题解
方法一
知识点:背包dp。
此题01背包很好做但不优化会超时。
首先设置状态 为考虑了 首,时长模 的余数为 的情况是否存在。模的操作是一个很关键的操作,优化了算法并最大化信息有效率,因为我们知道余数就可以知道是否整点了。
注意因为成功条件就是余数为 ,我们不能一开始给 初值为 ,但后果是某首自己的时间没法直接进入,因为无法从 转移,因此要特判。
但这样还不能过,需要有个跳出条件,显然 时可以直接跳出。
还有个优化,但这里可以不需要, 时一定能成功,因为有根据抽屉原理,此时存在两个不同端点的时间余数前缀和是相同的,于是以这两个端点的连续区间和是 的倍数,即余数为 。
最终还能滚动数组优化空间。
时间复杂度
空间复杂度
方法二
知识点:背包dp,STL。
由于是一个 01背包中的01类型,因此可以 bitset
常数优化。写起来也简单。
时间复杂度
空间复杂度
代码
方法一
#include <bits/stdc++.h> #define ll long long using namespace std; bool dp[2][3607]; int a[100007]; bool solve() { int n; cin >> n; for (int i = 1;i <= n;i++) cin >> a[i], a[i] %= 3600; if (n >= 3600) return true;///必然能得到连续的和为3600倍数的(这道题没这个特判也行) memset(dp, 0, sizeof(dp)); for (int i = 1;i <= n;i++) { dp[i & 1][a[i]] = 1;///由于j=0保留为0,所以从j=0传递特判 for (int j = 0;j <= 3599;j++) { dp[i & 1][j] |= dp[i + 1 & 1][j] | dp[i + 1 & 1][(j - a[i] + 3600) % 3600]; } if (dp[i & 1][0]) return true;///优化 } return false; } int main() { std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; cin >> t; while (t--) { if (!solve()) cout << "NO" << '\n'; else cout << "YES" << '\n'; } return 0; }
方法二
#include <bits/stdc++.h> #define ll long long using namespace std; int a[100007]; bool solve() { int n; cin >> n; for (int i = 1;i <= n;i++) cin >> a[i], a[i] %= 3600; if (n >= 3600) return true;///必然能得到连续的和为3600倍数的(这道题没这个特判也行) bitset<3607> dp; for (int i = 1;i <= n;i++) { dp |= dp << a[i] | dp >> (3600 - a[i]);///多出去那部分也要 dp[a[i]] = 1;///单独考虑 } return dp[0]; } int main() { std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; cin >> t; while (t--) { if (!solve()) cout << "NO" << '\n'; else cout << "YES" << '\n'; } return 0;
本文来自博客园,作者:空白菌,转载请注明原文链接:https://www.cnblogs.com/BlankYang/p/16581448.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧