two points

#include<stdio.h>
int mon[100005];
int main() {
int n,m,sum,maxn,i;
while (scanf ("%d %d",&n,&m)!=EOF) {
sum=0;
maxn = 0;
for (i=0; i<n; i++) {
scanf ("%d",&mon[i]);
sum+=mon[i]; //The upper limit is divided into a heap
if (mon[i]>maxn) maxn=mon[i]; //The lower limit is divided into n heaps
}
while (maxn<sum) {
int count=0;
int mid=(maxn+sum)/2;
int ss=0;
for (i=0; i<n; i++) {
ss+=mon[i];
if (ss>mid) {  //分堆
ss=mon[i];
count++; //Number of heaps
}
}
if (count<m) sum=mid; //Interpretation of too many or too few heaps
else maxn=mid+1;
}
printf ("%d\n",maxn);
}
return  0;

}

Binary search pay attention to upper and lower bound determination and end condition

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325393286&siteId=291194637