POJ - 3273 Monthly Expense(二分)

来源:互联网 发布:hello软件 编辑:程序博客网 时间:2024/06/06 03:20
Monthly Expense
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 22382 Accepted: 8764

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

Line 1: Two space-separated integers: N and M 
Lines 2..N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day

Output

Line 1: The smallest possible monthly limit Farmer John can afford to live with.

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 spends at most $500 in any month. Any other method of scheduling gives a larger minimum monthly limit.


题意:给你n个数,让分成m组,每组必须是连续的一些数,求每组的和的最大值最小。

确定好数据的上界为所有数的和,数据的下届为最大的数,如此用二分一一处理即可,注意这里的数据可能会超int,所有用long long,同时最后分组的情况要加入进来,不要遗漏了。

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long LL;const int MAXN = 1e5 + 5;const int INF = 0x3f3f3f3f;int N, M;LL A[MAXN];bool C(int m) {LL res = 0, va = 0;for(int i = 0; i < N; i ++) {va += A[i];if(va > m) {va = A[i];res ++;}if(res > M) return false;}res ++;if(res > M) return false;return true;}int main() {while(~scanf("%d%d", &N, &M)) {LL sum = 0, ma = 0;for(int i = 0; i < N; i ++) {scanf("%lld", &A[i]);sum += A[i];ma = max(ma, A[i]);}LL lb = ma - 1, ub = sum;while(ub - lb > 1) {int mid = (ub + lb) >> 1;if(C(mid)) {ub = mid;} else {lb = mid;}}printf("%lld\n", ub);}return 0;}


1 0
原创粉丝点击