AC代码
2023-09-20 18:05:59
发布于:广东
14阅读
0回复
0点赞
#include <iostream>
#include <algorithm>
using namespace std;
int a[100005];
int A(int n,unsigned long long k) {
int cnt=0;
unsigned long long sum=0;
for (int i=0;i<n;i++) {
if (sum+a[i]>k) {
cnt++,sum=0;
}
sum+=a[i];
}
cnt++;
return cnt;
}
int main() {
int n,m,ma=-1e9;
unsigned long long sum=0;
scanf("%d %d",&n,&m);
for (int i=0;i<n;i++) {
scanf("%d",&a[i]);
sum+=a[i];
ma=max(ma,a[i]);
}
unsigned long long l=ma,r=sum;
while (l<=r) {
unsigned long long mid=(l+r)>>1;
unsigned long long tmp=A(n,mid);
if (tmp>m) {
l=mid+1;
}else {
r=mid-1;
}
}
printf("%d",l);
return 0;
}
这里空空如也
有帮助,赞一个