列表

详情


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 T(1≤T≤60), represents there are T test cases. 
For each test case: 
The first line contains an integer n(1≤n≤105), indicating there are n songs. 
The second line contains n integers a1,a2…an (1≤ai≤109 ), the ith integer ai indicates the ith song lasts ai 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

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 1500ms, 内存消耗: 6016K, 提交时间: 2020-04-15 16:14:47

#include<bits/stdc++.h>
using namespace std;
int t,x;
int n;
bitset<10000> cur;
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		cur.reset();
		cur[0]=1;
		for(int i=1;i<=n;i++)
		{
			cin>>x;x%=3600;
			cur|=cur<<x;
			cur|=cur>>3600;
		}
		if(cur[3600]) cout<<"YES"<<endl;
		else cout<<"NO"<<endl; 
	}	
}

C++11(clang++ 3.9) 解法, 执行用时: 533ms, 内存消耗: 2944K, 提交时间: 2020-04-15 12:26:38

#include<bits/stdc++.h>
using namespace std;
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int x,n;
		scanf("%d",&n);
		bitset<3600>q;
		for(int i = 0;i<n;i++){
			scanf("%d",&x);
			x%=3600;
			q|=q<<x|q>>(3600-x);
			q[x] = 1;
		}
		if(q[0])printf("YES\n");
		else printf("NO\n");
	}
}

pypy3(pypy3.6.1) 解法, 执行用时: 753ms, 内存消耗: 41352K, 提交时间: 2020-04-14 20:20:32

for _ in range(int(input())):
    n = int(input())
    A = map(int, input().split())
    S = {0}
    for a in A:
        if -a % 3600 in S:
            print("YES")
            break
        S = S | {(s + a) % 3600 for s in S}
    else:
        print("NO")
    
    

上一题