Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 19597 | Accepted: 7748 |
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
Lines 2..N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day
Output
Sample Input
7 5 100 400 300 100 500 101 400
Sample Output
500
Hint
题意:给定连续N天每天要花的钱,一个“fajomonths”由连续的天组成。现在把N天分成不超过M个"fajomonths",要求所有"fajomonths'里面的最大花费最小化。
二分。。。
AC代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define INF 0x3f3f3f
#define eps 1e-8
#define MAXN (100000+1)
#define MAXM (100000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
int a[MAXN];
bool judge(int o, int n, int m)
{
int cnt = 1; LL sum = 0;
for(int i = 1; i <= n; i++)
{
if(sum + a[i] > o)
{
cnt++;
sum = a[i];
}
else
sum += a[i];
}
return cnt <= m;
}
int main()
{
int N, M;
while(scanf("%d%d", &N, &M) != EOF)
{
int l = 0, r = 0;
for(int i = 1; i <= N; i++)
{
Ri(a[i]);
r += a[i];
l = max(a[i], l);
}
int ans = 0;
while(l <= r)
{
LL mid = (l + r) >> 1;
if(judge(mid, N, M))
{
ans = mid;
r = mid-1;
}
else
l = mid+1;
}
Pi(ans);
}
return 0;
}