B Beats 题解

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−1​lj​<kL but ∑j=1ilj>kL∑j=1i​lj​>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;
				}
        
	}	
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值