题目链接

题目

题目描述

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 \(T(1≤T≤60)\) , represents there are \(T\) test cases.

For each test case:

The first line contains an integer \(n(1≤n≤10^5)\) , indicating there are \(n\) songs.

The second line contains \(n\) integers \(a_1,a_2…a_n (1≤a_i≤10^9 )\) , the ith integer \(a_i\) indicates the ith song lasts \(a_i\) 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[i][j]\) 为考虑了 \(i\) 首,时长模 \(3600\) 的余数为 \(j\) 的情况是否存在。模的操作是一个很关键的操作,优化了算法并最大化信息有效率,因为我们知道余数就可以知道是否整点了。

注意因为成功条件就是余数为 \(0\) ,我们不能一开始给 \(0\) 初值为 \(1\) ,但后果是某首自己的时间没法直接进入,因为无法从 \(0\) 转移,因此要特判。

但这样还不能过,需要有个跳出条件,显然 \(dp[i][0] = 1\) 时可以直接跳出。

还有个优化,但这里可以不需要,\(n\geq3600\) 时一定能成功,因为有根据抽屉原理,此时存在两个不同端点的时间余数前缀和是相同的,于是以这两个端点的连续区间和是 \(3600\) 的倍数,即余数为 \(0\) 。

最终还能滚动数组优化空间。

时间复杂度 \(O(n)\)

空间复杂度 \(O(1)\)

方法二

知识点:背包dp,STL。

由于是一个 01背包中的01类型,因此可以 bitset 常数优化。写起来也简单。

时间复杂度 \(O(n)\)

空间复杂度 \(O(1)\)

代码

方法一

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

NC13885 Music Problem的更多相关文章

  1. 1199 Problem B: 大小关系

    求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...

  2. No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.

    Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...

  3. C - NP-Hard Problem(二分图判定-染色法)

    C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:262144 ...

  4. Time Consume Problem

    I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...

  5. Programming Contest Problem Types

        Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...

  6. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  7. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

  8. [LeetCode] Water and Jug Problem 水罐问题

    You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...

  9. [LeetCode] The Skyline Problem 天际线问题

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  10. PHP curl报错“Problem (2) in the Chunked-Encoded data”解决方案

    $s = curl_init(); curl_setopt($s, CURLOPT_POST, true); curl_setopt($s, CURLOPT_POSTFIELDS, $queryStr ...

随机推荐

  1. Linux复制安装 jdk 环境

    转载请注明出处: 最近在弄服务器环境,发现可以通过复制已安装 jdk 的服务器配置到新的服务器,并配置服务器环境变量配置文件就可以完成. 操作步骤如下: 1. 查看以安装jdk服务器的环境配置,并复制 ...

  2. Telegraf 使用小结

    转载请注明出处: 1.简介: Telegraf是一个开源的代理程序,用于收集.处理.汇总和发送指标数据.它可以与不同的数据存储和可视化工具(如InfluxDB.Elasticsearch.Grafan ...

  3. Java 如何将Excel转换为TXT文本格式

    TXT文件是一种非常简单.通用且易于处理的文本格式.在处理大规模数据时,将Excel转为TXT纯文本文件可以提高处理效率.此外,许多编程语言和数据处理工具都有内置的函数和库来读取和处理TXT文件,因此 ...

  4. 他凌晨1:30给我开源的游戏加了UI|模拟龙生,挂机冒险

    一.前言 新年就要到了,祝大家新的一年: 龙行龘龘, 前程朤朤! 白泽花了点时间,用 800 行 Go 代码写了一个控制台的小游戏:<模拟龙生>,在游戏中你将模拟一条新生的巨龙,开始无尽的 ...

  5. [转帖]基于MySQL8.0存储过程实现myawr平台的top sql功能

    概述 众所周知,MySQL数据库中的performance_schema的事件统计表中的统计数据计算的是累计值,如果想要计算某段时间的TOP SQL是不行的,这里考虑用函数定期取值存进中间表定期将累计 ...

  6. TiKV 服务部署的注意事项

    TiKV 服务部署的注意事项 背景 最近发现tikv总是会掉线 不知道是哪里触发了啥样子的bug. 所以想着使用systemd 管理一下, 至少在tikv宕机的时候能够拉起来服务. 二进制文件 pd- ...

  7. [转帖]linux查看端口及端口详解

    https://www.cnblogs.com/the-tops/p/6126941.html   今天现场查看了TCP端口的占用情况,如下图   红色部分是IP,现场那边问我是不是我的程序占用了tc ...

  8. [转帖]编译安装goofys挂载Scaleway免费75G对象存储

    日常•2022年5月29日   goofys编译 goofys是一个开源的使用Go编写的s3存储桶挂载工具,主打高性能.由于使用Go编写,没有用到什么特别的依赖,自己编译也很容易.截止2022.5.2 ...

  9. [转帖]Linux Shell:date日期时间操作

    https://www.jianshu.com/p/cc9ebb212a8e 整理Linux Shell脚本中常用的日期操作,给予date命令,主要用法总结 获得当前日期,时间戳,date,date ...

  10. 【转帖】ChatGPT的前身:InstructGPT

    https://www.jianshu.com/p/6daf35cbc46a ChatGPT的论文目前还没有发布,在其官方博客(https://openai.com/blog/chatgpt/)中对方 ...