Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 20090 | Accepted: 7919 |
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 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
Output
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.
题意:将序列划分为k段,要求找出k段中最大的资金和要在所有符合要求的划分中值最少
思路: 我们可以直到这个值必然在所有数中最大值与所有数的总和之间,那么只要再这个区间进行二分即可
#include <cstdio> #include <iostream> #include <algorithm> using namespace std; const int maxn = 100000 + 10; int main() { int n, m; int a[maxn]; int max1, cnt, s, sum; scanf("%d%d", &n, &m); sum = 0, max1 = 0; for (int i = 0; i < n; i++){ scanf("%d", &a[i]); sum = sum + a[i]; if (a[i] > max1) max1 = a[i]; } while (max1 < sum){ s = 0, cnt = 0; int middle = (max1 + sum) / 2; for (int i = 0; i < n; i++){ s = s + a[i]; if (s > middle){ s = a[i]; cnt++; } } if (cnt < m) sum = middle; else max1 = middle + 1; } printf("%d\n", max1); return 0; }