Monthly Expense
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 8261 |
|
Accepted: 3399 |
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 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.
题意很简单,算法也很简单,以前这种题目绝对做不出来,可能没做过这种类型的吧
现在看貌似很简单,
做法就是二分枚举答案+贪心验证
二分的下界取最大的数
上界取所有数的和即可
唔,昨晚上刚看到这题的时候,想着dp应该是可以的
不过测试数据是10w,又不行了,然后想着可以转化成图论的模型,不过点太多,还是不行
然后搜索,dp都不行了,搜索也白搭,然后想二分,然后算了下只有10^9 ,顶多30多次 ,貌似可以
…………
#include<stdio.h>
#include<string.h>
#include<math.h>
#define maxn 100005
int n,m,a[maxn];
int lower,upper;
int max(int a,int b)
{
return a>b?a:b;
}
bool yanz(int x)
{
int num,i,tmp;
num=0;
i=1;
while(i<=n)
{
tmp=0;
while(tmp+a[i]<=x&&i<=n)
{
tmp=tmp+a[i];
i++;
}
num++;
}
if(num>m) return 0;
return 1;
}
int main()
{
int i;
while(scanf("%d%d",&n,&m)!=EOF)
{
lower=-1;
upper=0;
for(i=1; i<=n; i++)
{
scanf("%d",&a[i]);
lower=max(a[i],lower);
upper=upper+a[i];
}
//printf("%d %d\n",upper,lower);
int left,right;
left=lower;
right=upper;
while(left<right)
{
int mid=(left+right)/2;
if(yanz(mid)) right=mid; //printf("%d ok\n",right);}
else left=mid+1;
}
printf("%d\n",left);
}
return 0;
}
wa了两遍,发现最开始的时候tmp没初始化