Monthly expense
Time limit:2000 ms |
|
Memory limit:65536 K |
Total submissions:14158 |
|
Accepted:5697 |
Description
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 nextN(1 ≤N≤ 100,000) days.
FJ wants to create a budget for a sequential set of exactlyM(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:
NAnd
M
Lines 2 ..
N+ 1: Line
I+ 1 contains the number of dollars Farmer John sort Ds on
ITh day
Output
Line 1: the smallest possible monthly limit Farmer John can afford to live.
Sample Input
7 5100400300100500101400
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 has Ds at most $500 in any month. any other method of scheduling gives a larger minimum monthly limit.
Source
Usaco 2007 March silver
Solution:
Given a sequence composed of N numbers, it is divided into M consecutive intervals. All elements in each interval are added to get m sums. m sums and must have a maximum value, we require that the maximum value be as small as possible.
Binary Search can solve this problem. The framework of this type of problem is to find the lower left and upper right, while (left <right), and find the MID. The mid value conforms to the meaning of the question and continues to be divided into two parts. The last right is the answer.
The lower bound of this question is the maximum value of N numbers, because at this time, it is necessary to divide it into N intervals (that is, one number and one interval), and left is the maximum value of N intervals and of the meaning of the question, the sum of all intervals in the previous session. At this time, it is necessary to divide the number into one interval (all numbers are in one interval), 1 <= m <= n, therefore, the required value must be between [left, right. For each mid, the number of N records is traversed. It can be divided into several intervals. If the number of intervals is smaller than (or equal to) the given m, the upper bound is greater, the other right = mid, otherwise the other left = Mid + 1.
Code:
# Include <iostream> # include <stdio. h> # include <string. h> using namespace STD; const int maxn = 100010; int money [maxn]; int n, m; int main () {scanf ("% d", & N, & M); int left =-1, Right = 0; For (INT I = 1; I <= N; I ++) {scanf ("% d ", & money [I]); If (left <money [I]) Left = money [I]; right + = money [I];} while (left <right) {int mid = (left + right)/2; int CNT = 0; int cost = 0; For (INT I = 1; I <= N; I ++) {If (cost + money [I]> mid) {CNT ++; // interval, excluding the current money [I] cost = money [I];} else cost + = money [I];} CNT ++; // The last cost value also occupies the day if (CNT <= m) Right = mid; else left = Mid + 1 ;}cout <right <Endl; return 0 ;}
[ACM] poj 3273 monthly expense (minimizes the maximum value of a binary solution)