Monthly Expense POJ - 3273
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
Line 1: Two space-separated integers: N and M
Lines 2.. N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day
Lines 2.. N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day
Output
Line 1: The smallest possible monthly limit Farmer John can afford to live with.
Sample Input
7 5 100 400 300 100 500 101 400
Sample Output
500
Hint
If Farmer John schedules the months so that the first two days are a month, the third and fourth are a month, and the last three are their own months, he spends at most $500 in any month. Any other method of scheduling gives a larger minimum monthly limit.
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<string> 5 #include<cstring> 6 #include<cmath> 7 #define MAX 100005 8 using namespace std; 9 typedef long long ll; 10 11 int n,m; 12 int a[MAX]; 13 14 bool check(double ans) 15 { double temp=0,t=0; 16 for(int i=1;i<=n;i++){ 17 temp+=a[i]; 18 if(temp>ans){ 19 t++; 20 temp=a[i]; 21 } 22 } 23 t++; 24 if(t<=m) return true; 25 else return false; 26 } 27 int main() 28 { while(~scanf("%d%d",&n,&m)){ 29 int ma=0,sum=0; 30 for(int i=1;i<=n;i++){ 31 scanf("%d",&a[i]); 32 ma=max(ma,a[i]); 33 sum+=a[i]; 34 } 35 double l=ma,r=sum; 36 double mid; 37 while(r-l>1e-9){ 38 mid=(l+r)/2.0; 39 if(check(mid)) r=mid; 40 else l=mid; 41 } 42 printf("%d\n",int(l+0.5)); 43 } 44 }