| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 15876 | Accepted: 6346 |
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 400
Sample Output
500
Hint
#include <cstdio>
const int maxn=100010;
int a[maxn];
int main()
{
int n,m,i,mid,high=0,low=0,sum,t;
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
high+=a[i];
if(low<a[i])
low=a[i];
}
mid=(high+low)/2;
while(low<high)
{
t=1,sum=0;
for(i=0;i<n;i++)
{
if(sum+a[i]<=mid)
sum+=a[i];
else
{
sum=a[i];
t++;
}
}
if(t>m)
low=mid+1;
else
high=mid-1;
mid=(high+low)/2;
}
printf("%d\n", mid);
return 0;
}
本文介绍了一个经典的时间复杂度为O(n log n)的预算划分问题,通过二分枚举的方法来解决如何将连续的几天划分为若干组以使得每组的最大开销最小的问题。
177

被折叠的 条评论
为什么被折叠?



