| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 11178 | Accepted: 4579 |
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
=============================
题意:给出农夫每天的花费,连续分组使各组的花费之和尽量小,输出最大值
对最大值进行二分
#include <iostream>
#include <cstdio>
using namespace std;
int n,m,a[111111],MAX=-1,sum=0;
bool C(int x)
{
int cnt=0;
for(int i=-1;i<n;)
{
int ans=0;
while(ans<=x)
{
i++;
if(i==n) break;
ans+=a[i];
//cout<<a[i]<<" "<<i<<" "<<ans<<endl;
}
if(i!=n) i--;
cnt++;
}
if(cnt<=m) return 1;
else return 0;
}
int b_search()
{
int l=MAX,r=sum,mid,ans=0;
while(l<=r)
{
mid=(l+r)/2;
if(C(mid))
{
ans=mid;
r=mid-1;
}
else l=mid+1;
}
return ans;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(MAX<a[i]) MAX=a[i];
sum+=a[i];
}
//cout<<MAX<<" "<<sum<<endl;
int ans=b_search();
printf("%d\n",ans);
return 0;
}

本文介绍了一个经典的预算划分问题,即如何将连续的预算周期划分成若干个连续的时间段(fajomonths),使得每个时间段内的最大支出最小化。通过使用二分查找的方法,有效地解决了这个问题,并提供了完整的代码实现。
3267

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



