Description
Farmer John isAn astounding Accounting wizard and have realized he might run outof money to run the farm. He has already calculated and recorded the exact amount of money (1≤moneyi≤Ten, the) that he would need to spend each day over the next N (1≤n≤ -, the) days. FJ wants to create a budget forA sequentialSetof exactly M (1≤m≤n) Fiscal periods called"fajomonths". Each of these fajomonths contains aSetOf1or more consecutive days. Every day isContainedinchexactly 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
1: Both space-2. n+1: Line i+1 contains the number of dollars Farmer John spends on the ith day
Output
1: The smallest possible monthly limit Farmer John can afford to live with.
Sample Input
7 5 - - - - - 101 -
Sample Output
500
Hint
If Farmer John Schedules The months so, the first is a month, the third and fourth is a month, and the last Three is their own months, he spends at the most $ on any month. Any other method of scheduling gives a larger minimum monthly limit.
Source
Usaco March Silver
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 #definell Long Long6 #defineN 1000067 #defineINF 1<<308 intn,m;9 intA[n];Ten BOOLSolveintx) { One intsum=0; A intgroup=0; - for(intI=0; i<n;i++){ - if(sum+a[i]<=x) { thesum+=A[i]; - } - Else{ -sum=A[i]; +group++; - } + } A if(group>=m)return false;//if the current mid value can be divided into groups greater than or equal to m, it means that mid is too small and the number of groups becomes smaller when mid becomes larger. at return true; - } - intMain () - { - - while(SCANF ("%d%d", &n,&m) = =2){ in intHigh=0; - intlow=0; to for(intI=0; i<n;i++){ +scanf"%d",&a[i]); -high+=A[i]; the if(a[i]>Low ) { *Low=a[i];//Take the most expensive day as the lowest low (the equivalent of n talent to n groups) $ }Panax Notoginseng } - the + while(low<High ) { A the intMid= (Low+high) >>1; + if(Solve (mid)) { -High=mid; $ } $ Else{ -Low=mid+1; - } the } -printf"%d\n", low);Wuyi } the return 0; -}
View Code
POJ 3273 Monthly Expense (maximum minimum value of binary search)