NC13885. Music Problem
描述
输入描述
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.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")