求助20分
查看原帖
求助20分
740350
FS_qwq楼主2022/12/14 17:46

哪里错了?

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int a[maxn],N,M;
bool check(long long x){
	long long sum=0;
	int tot=0,i;
	for(i=1;i<=N;i++){
		if(sum+a[i]>x){
			tot++;
			sum=a[i];
			if(tot>M)return false;
		}
		else sum+=a[i];
	}
	return true;
} 
int main(){
	ios::sync_with_stdio(false);
	int i;
	long long l=0,r=0,mid,ans;
	cin>>N>>M;
	for(i=1;i<=N;i++){
		cin>>a[i];
		r+=a[i];
		l=max(l,(long long)a[i]);
	}
	while(r>l){
		mid=(l+r)/2;
		if(check(mid)){
			ans=mid;
			r=mid;
		}
		else l=mid+1;
	}
	cout<<ans<<endl;
	return 0;
}
2022/12/14 17:46
加载中...