Question 1639: [usaco 2007 Mar] monthly expense time limit: 5 sec memory limit: 64 MB
Description
Farmer John is a surprising accounting genius and he understands that he may spend his money to keep the farm running properly every month. He has calculated the cost of moneyi (1 <= moneyi <= 100,000) per day in N (1 <= n <= 10,000) working days ), he wants to make a budget for his consecutive M (1 <= m <= N) checkout period called "liquidation month, each "liquidation month" includes one working day or more consecutive working days, and each working day is only included in one "liquidation month. FJ's goal is to arrange these "liquidation months" to minimize the maximum expense of each liquidation month to determine his monthly expenditure limit.
Input
First line: two integers separated by spaces: N and m
2nd. n + 1 row: I + 1 contains the cost of Fj on his I + 1 business day
Output
Row 1: The amount of money that can maintain normal farm operation every month
Sample input7 5
100
400
300
100
500
101
400
Sample output500
Input details
There are seven working days divided by five "liquidation months. He spent 100,400,100,500,101 yuan and 400 yuan on his work day.
Output details
If FJ schedules his monthly budget, he will divide the first two days into one month, the third day and the fourth day into one month, and the last three working days are respectively in one month, therefore, he spends a maximum of 500 yuan a month. Other methods always produce a large result.
100 400 300 100 500 101 400 daily spending
--- 1 --- 2 ----3--4--5-monthly label
500 400 500 101 400 monthly spending
Question
Hhh, I can always find the problem at a glance! This is the final answer of the second question, and then try to do it!
Code
1 /*Author:WNJXYK*/ 2 #include<cstdio> 3 using namespace std; 4 int n,m; 5 int money[100010]; 6 inline bool check(int x){ 7 int cnt=1; 8 int sum=0; 9 for (int i=1;i<=n;i++){10 if (money[i]>x) return false;11 if (sum+money[i]>x){12 cnt++;13 if (cnt>m) return false;14 sum=money[i];15 continue;16 }17 sum+=money[i];18 }19 if (cnt<=m) return true; 20 } 21 int main(){22 scanf("%d%d",&n,&m);23 for (int i=1;i<=n;i++) scanf("%d",&money[i]);24 int maxm=money[1];25 int left=0,right=1000000000,mid;26 while(left<right){27 int mid=(left+right)/2;28 if (check(mid)==false){29 left=mid+1;30 }else{31 right=mid;32 }33 }34 printf("%d\n",right);35 return 0;36 }View code
Bzoj 1639: [usaco2007 Mar] monthly expense monthly expenditure