20分玄关求调!!
查看原帖
20分玄关求调!!
1343286
jebfghbbcdsl楼主2025/1/19 20:39

只AC了#4

#include<cstdio>
int n,m,maxa,suma,a[100010];
inline int max(int x,int y){
	return x>y?x:y;
}
bool check(int x){
	int sum=0,cnt=0;
	for(int i=1;i<=n;i++){
		if(sum+a[i]>x)
			cnt++,sum=a[i];
		else sum+=a[i];
	}return cnt<=m;
}
int Find(){
	int l=maxa,r=suma;
	while(l<=r){
		int mid=l+r>>1;
		if(check(mid))r=mid-1;
		else l=mid+1;
	}
	return l;
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		maxa=max(maxa,a[i]);
		suma+=a[i];
	}
	printf("%d",Find());
	return 0;
}
2025/1/19 20:39
加载中...