Monthly Expense(二分--最小化最大值)
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
Lines 2.. N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day
Output
Sample Input
7 5 100 400 300 100 500 101 400
Sample Output
500
Hint
1 #include <iostream> 2 #include<cstring> 3 #include<cmath> 4 #include<cstdio> 5 #include<algorithm> 6 using namespace std; 7 typedef long long ll; 8 const int MAXN=1e5+10; 9 const int Inf=0x3f3f3f3f; 10 int m,n; 11 int str[MAXN]; 12 int solve(int mid) 13 { 14 int sum=0,cnt=1; 15 for(int i=0;i<m;i++) 16 { 17 if(sum+str[i]<=mid) 18 { 19 sum+=str[i]; 20 } 21 else 22 { 23 sum=str[i]; 24 cnt++; 25 } 26 } 27 if(cnt>n) 28 return 0; 29 return 1; 30 } 31 int main() 32 { 33 while(scanf("%d%d",&m,&n)!=-1) 34 { int left=0,right=0,mid; 35 for(int i=0;i<m;i++) 36 { 37 scanf("%d",&str[i]); 38 right+=str[i]; 39 left=max(left,str[i]); 40 } 41 while(left<=right) 42 { 43 mid=(right+left)/2; 44 if(solve(mid)) 45 { 46 right=mid-1; 47 } 48 else 49 { 50 left=mid+1; 51 } 52 } 53 printf("%d\n",mid);} 54 return 0; 55 }
本文来自博客园,作者:左手边五十米,转载请注明原文链接:https://www.cnblogs.com/moomcake/p/9426856.html