80分求解
查看原帖
80分求解
1495558
ygfsnd楼主2025/1/19 11:37
#include<bits/stdc++.h>
using namespace std;
int a[100010],b[100010];
int n,m,res;
bool check(int x){
int sum,num=1;
	sum=0;
	for(int i=1;i<=n;i++){
		if(b[i]-sum>x){
			sum=b[i-1];
			num++;
}
		if(num>m) return false;
	}
	return true;
}
int BinSearch(int l,int r){
	int ans,low=1,high=r;
	while(low<=high){
		int mid=(low+high)>>1;
		if(check(mid)){
			ans=mid;
			high=mid-1;
		}
		else low=mid+1;
	}
	return ans;
}
int main(){
	int minn=0;
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		if(a[i]>minn) minn=a[i];
		b[i]=b[i-1]+a[i];
	}
	res=BinSearch(minn,b[n]);
	cout<<res<<endl;
	return 0;
}
2025/1/19 11:37
加载中...