牛客算法周周练2 - Music Problem(DP)

链接:https://ac.nowcoder.com/acm/contest/5203/B
来源:牛客网

时间限制: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

题意:

判断一个序列中是否有子序列的和能够被3600整除。

另外有一道相似的题,思想是一样的

判断一个序列里是否有子序列的和能够被k整除,求出这样的最长的子序列的长度。

题解:https://www.cnblogs.com/jiamian/p/12764957.html

 1 #include <bits/stdc++.h>
 2 typedef long long LL;
 3 #define pb push_back
 4 const int INF = 0x3f3f3f3f;
 5 const double eps = 1e-8;
 6 const int mod = 1e9+7;
 7 const int maxn = 1e5+10;
 8 using namespace std;
 9 
10 int a[maxn];
11 int dp[3605];//dp[j]=1表示结果为j的出现过,=0表示没出现过
12 int temp[3605];//保存上一次的dp结果,相当于上一次的dp数组
13 
14 int main()
15 {
16     #ifdef DEBUG
17     freopen("sample.txt","r",stdin); //freopen("data.out", "w", stdout);
18     #endif
19     
20     int T;
21     scanf("%d",&T);
22     while(T--)
23     {
24         memset(dp,0,sizeof(dp));
25         memset(temp,0,sizeof(temp));
26         int n;
27         scanf("%d",&n);
28         for(int i=1;i<=n;i++)
29         {
30             scanf("%d",&a[i]);
31             a[i]%=3600;
32         }
33         for(int i=1;i<=n;i++)
34         {
35             for(int j=0;j<3600;j++)
36             {
37                 if(j==0||temp[j]) dp[(a[i]+j)%3600]=1;
38                 if(dp[0]) break;
39             }
40             if(dp[0]) break;//找到就break掉
41             for(int j=0;j<3600;j++)
42                 temp[j] = dp[j];
43         }
44         printf(dp[0]?"YES\n":"NO\n");
45     }
46     
47     return 0;
48 }

-

猜你喜欢

转载自www.cnblogs.com/jiamian/p/12764972.html
今日推荐