题目链接

题目

题目描述

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. Skywalking 搭建 nacos 注册中心及mysql 存储的集群架构

    本文为博主原创,未经允许不得转载 Skywalking 集群是将skywalking oap作为一个服务注册到nacos上,只要skywalking oap服务没有全部宕机,保证有一个skywalki ...

  2. 基于python的租房网站-房屋出租租赁系统(python+django+vue)

    该项目是基于python/django/vue开发的房屋租赁系统/租房平台,作为本学期的课程作业作品.欢迎大家提出宝贵建议. 功能介绍 平台采用B/S结构,后端采用主流的Python+Django进行 ...

  3. Nacos源码 (6) Grpc概述与Nacos集成

    Nacos 2.x版本增加了GRPC服务接口和客户端,极大的提升了Nacos的性能,本文将简单介绍grpc-java的使用方式以及Nacos中集成GRPC的方式. grpc-java GRPC是goo ...

  4. P1914 小书童——凯撒密码

    1.题目介绍 小书童--凯撒密码 题目背景 某蒟蒻迷上了 "小书童",有一天登陆时忘记密码了(他没绑定邮箱 or 手机),于是便把问题抛给了神犇你. 题目描述 蒟蒻虽然忘记密码,但 ...

  5. 【css】 text-align 居中导航

    原理 :利用 inline-block 将 导航 作为 文本 , 被外层具有 text-align 属性的导航盒子包含 .从而实现居中效果 1.  html 结构 <header> < ...

  6. [转帖]Jmeter学习笔记(九)——响应断言

    Jmeter学习笔记(九)--响应断言 https://www.cnblogs.com/pachongshangdexuebi/p/11571348.html Jmeter中又一个元件叫断言,用于检查 ...

  7. [转帖]Redis各版本特性汇总

    redis4 redis5 redis6 redis6.2 重大特性 1.模块系统 2.PSYNC2 3.LFU淘汰策略 4.混合RDB-AOF持久化 5.LAZY FREE延迟释放 6.MEMORY ...

  8. PG数据库恢复简单记录

    公司同事给了一个很小的数据 我这边进行备份和恢复操作 第一步 创建数据库 su - postgres #进入pg数据库的用户 psql #输入密码 登录 create user demo with p ...

  9. node中的优先从缓存中加载模块与模块的加载规则

    执行 node main.js 请问 b模块会被加载几次 //main.js require('./a.js') var fn = require('./b.js') console.log(fn.s ...

  10. jenkins 安装与构建过程中的系列问题

    一.插件安装遇到的依赖问题 插件安装分为在线安装和离线安装 1.在线安装 搜索要安装的插件,然后进行安装即可 2.离线安装hpi文件 使用该方法安装插件每次只能安装一个插件,且如果插件之间存在依赖性则 ...