Monthly Expense(二分--最小化最大值)
Posted moomcake
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 ",mid);} 54 return 0; 55 }
以上是关于Monthly Expense(二分--最小化最大值)的主要内容,如果未能解决你的问题,请参考以下文章
POJ 3273 Monthly Expense (二分&最大化最小值)
POJ-3273 Monthly Expense---最小化最大值