Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already calculated and recorded the exact amount of money (1 ≤ moneyi ≤ 10,000) that he will need to spend each day over the next N (1 ≤ N ≤ 100,000) days.
FJ wants to create a budget for a sequential set of exactly M (1 ≤ M ≤ N) fiscal periods called "fajomonths". Each of these fajomonths contains a set of 1 or more consecutive days. Every day is contained in exactly one fajomonth.
FJ‘s goal is to arrange the fajomonths so as to minimize the expenses of the fajomonth with the highest spending and thus determine his monthly spending limit.
Input
Output
Sample Input
7 5 100 400 300 100 500 101 400
Sample Output
500
Hint
1 #include<stdio.h> 2 #include<string.h> 3 #include<iostream> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 const int inf=0x3f3f3f3f3; 8 int v[100005],n,m,w[100005]; 9 int searchs(int low,int high) 10 { 11 int maxn=0; 12 while(low<=high) 13 { 14 // printf("**%d**%d\n",low,high); 15 int mid=(low+high)>>1; 16 // int temp=1; 17 // w[1]=v[1]; 18 // for(int i=2;i<=n;++i) 19 // { 20 // w[i]=v[i]; 21 // if(w[i]+w[i-1]<=mid) 22 // { 23 // w[i]+=w[i-1]; 24 // } 25 // else 26 // { 27 // ++temp; 28 // } 29 // } 30 int temp = 1,sum = 0; 31 for(int i=1; i<=n; i++) 32 { 33 if(sum+v[i] <= mid) 34 { 35 sum += v[i]; 36 } 37 else 38 { 39 temp++; 40 sum = v[i]; 41 } 42 } 43 if(temp==m) 44 { 45 // printf("%d*%d\n",mid,temp); 46 maxn=mid; 47 high=mid-1; 48 } 49 else if(temp>m) 50 { 51 // printf("%d**%d\n",mid,temp); 52 low=mid+1; 53 } 54 else high=mid-1,maxn=mid;//printf("%d***%d\n",mid,temp);; 55 } 56 return maxn; 57 } 58 int main() 59 { 60 61 scanf("%d%d",&n,&m); 62 int a=0,b=0; 63 for(int i=1; i<=n; ++i) 64 { 65 scanf("%d",&v[i]); 66 a+=v[i]; 67 b=max(b,v[i]); //这里可不能在b中存输入数据的最小值,因为题目是要让 68 } //我们求分组后的最大值,你这样可能求出来的值小于输入的最大值 69 int mid=500,temp=0; //因为当mid<v[i]的时候temp仅仅加了一,但是这并不符合题意 70 int pow=searchs(b,a); 71 printf("%d\n",pow); 72 }
原文:https://www.cnblogs.com/kongbursi-2292702937/p/10695495.html