其他分享
首页 > 其他分享> > 1022 Music Problem bitset 动态规划 线性DP

1022 Music Problem bitset 动态规划 线性DP

作者:互联网

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

题目描述

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

分析

题意是给n个音乐,每个音乐持续a[i] 的时间,问,任选多少首音乐,从12:00点开始听音乐,能不能在整点结束

1 h = 60 min = 3600 s 所以只要3600 就行了,将a[i] %= 3600;

n首音乐,随着音乐变化的就是之前的音乐能听到哪些时间嘛,粗略估计开个dp[N][3600] 数组 ,表示听到第i首,在第j秒是否能停止。

将dp[0][0] 初始化成1,表示第0首歌,能在第0秒停止。

但是N = 1e5 ,空间复杂度超了,然后要循环n首音乐,每个音乐要看3600个时段,时间复杂度也超了

所以要能压缩空间复杂度和时间复杂度 -> bitset<3610> b 函数 

当枚举到i 的时候,状态转移方程式: b = b | b << a[i] | b >> (3600 - a[i]) 表示: 能在上一首歌处停止 xor 这首歌的唱完后,能在哪些位置停止

注意 b >> ( 3600 - a[i] )  即将b 左移 a[i] - 3600,相当于取模 dp[i][((j - a[i]) % 3600 + 3600) % 3600];

 

易错点:

使用了dp[N] 数组,由于要

 

//-------------------------代码----------------------------

//#define int LL
const int N = 1e5+10;
int n,m;
int a[N];
void solve()
{
cin>>n;
// V<V<int>>mp(n+1,V<int>(m+1));
bitset<3610>b;
b.reset();
fo(i,1,n) {
cin>>a[i];
a[i] %= 3600;
}
//3600s = 60 min = 1h
b[0] = 1;
fo(i,1,n) {
b = b | b << a[i] | b >> (0-a[i] + 3600) ;
}
//fo(i,0,3600) {
// cout<<vis[i]<<' '<<i<<endl;
//}
bool ok = false;
if(b[3600]) ok = 1;
if(ok) {YES} else NO;
}

signed main(){
clapping();TLE;

int t;cin>>t;while(t -- )
solve();
// {solve(); }
return 0;
}

/*样例区


*/

//------------------------------------------------------------

标签:00,3600,1022,stop,13,Music,bitset,YES,he
来源: https://www.cnblogs.com/er007/p/16455143.html