Monthly Expense
Time Limit:2000 MS |
|
Memory Limit:65536 K |
Total Submissions:13281 |
|
Accepted:5362 |
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.
Question
It is also a binary question. In the next n days, Farmer John needs to spend money [I] every day to divide these days into k parts (each part is a consecutive day ), the sum of each part is as small as possible, and the smallest sum is output.
The second answer is still the second answer... However, it is particularly strange that if a res is used to maintain the current feasible value, WA will be used, and R must use r = mid instead of r = mid-1...
This dual point is really hard
Sample Code
/*****@author Shen*@title poj 3273*/#include
#include
#include
#include using namespace std;int n, k;int r, v[100005];int maxa = 0, mina = 0;bool test(int x){ int sum = 0, cnt = 1; for (int i = 0; i < n; i++) { if (sum + v[i] <= x) sum += v[i]; else cnt++, sum = v[i]; } //printf("\t%s with x = %d, result is that sum = %d.\n", __func__, x, sum); return cnt <= k;}int Bsearch(int l, int r){ while (l < r) { int mid = (r + l) / 2; //printf("l = %d, r = %d, mid = %d.\n", l, r, mid); if (test(mid)) r = mid; else l = mid + 1; } return l;}void solve(){ maxa = mina = 0; for (int i = 0; i < n; i++) { scanf("%d", &v[i]); maxa += v[i]; mina = max(mina, v[i]); } int ans = Bsearch(mina, maxa); printf("%d\n", ans);}int main(){ while (~scanf("%d%d", &n, &k)) solve(); return 0;}