Colin loves music but cannot catch the rhythm well, especially when counting beats! After hard research on music theory, Colin found a hidden theorem: There won't be a note crossing the boundary between the beats for modern songs.
To simplify the problem, we assume that a modern song consists of several sequential notes whose lengths are l1,l2,⋯ ,lnl1,l2,⋯,ln milliseconds. The notes will be played one by one, which means the song will start playing the first note at the time 00, and the second note at the beginning of the l1+1l1+1 milliseconds, and the third note at the beginning of the l1+l2+1l1+l2+1 milliseconds, and so on.
If the length of the beat is LL milliseconds, then there should not exist any note that is playing but not starting to play at the beginning of the kLkL milliseconds, for any non-negative integer kk. Formally, there should not exist any note ii such that ∑j=1i−1lj<kL∑j=1i−1lj<kL but ∑j=1ilj>kL∑j=1ilj>kL for any non-negative integer kk.
Now Colin has received a new song from Eva, and he wants to practice counting beats before he sings for Eva. To challenge himself, Colin wants to find the smallest possible length of a beat, such that it satisfies the theorem. Can you help him?
代码略微偏离正解,但被优化救回来了
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5+5;
int w[N];
int sum[N];
unordered_set<int>se;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int maxn=0;
for(int i =1;i<=n;i++){
cin >> w[i];
sum[i] = sum[i-1] + w[i];
se.insert(sum[i]);
maxn=max(w[i],maxn);
}
int flagn=1;
for(int i=1;i<=n;i++)
{
if(sum[i]>=maxn)
{
flagn=i;
break;
}
}
for(int i=flagn;i<=n;i++){
bool z =true;
int k_max = sum[n] / sum[i];
int num=sum[i];
for(int k=2;k<=k_max;k++){
num += sum[i];
if(se.find(num) == se.end() ){
z = false;
}
}
if(z){
cout << sum[i];
return 0;
}
}
}