Music Problem---枚举

3 篇文章 0 订阅
时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld

题目描述

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 i th integer a i indicates the i th 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

实现代码

#include<iostream>
#include<cstring>
using namespace std;

const int maxn = 1e5 + 5;

int vis[3600], a[100005], dp[3600];

bool judge(int len) {
	for (int i = 1; i <= len; i++) {
		dp[a[i]] = 1;
		if (dp[0]) return true;
		for (int j = 1; j < 3600; j++) {
			if (vis[j]) {
				dp[(a[i] + j) % 3600] += 1;
				if (dp[0]) return true;
			}
		}
		for (int j = 0; j < 3600; j++) if (dp[j]) vis[j] = 1; // 避免自己修改自己
	}
	return false;
}

int main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int n, len, num;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> len;
		memset(vis, 0, sizeof(vis));
		memset(dp, 0, sizeof(dp));
		for (int i = 1; i <= len; i++) {
			cin >> a[i];
			a[i] %= 3600;
		}
		if (judge(len)) cout << "YES\n";
		else cout << "NO\n";
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值