技改?gkd加强冰心和奶花
Description
There are n veges stand in line, Albert_s plan to punish them since they are too weak. The picture following below shows one of the veges waiting to be kicked.
Now Albert_s plan to kick all the veges on days in order. Since Kick the vege costs him RP, and the final cost is equal to the max cost of one day on the days.
Albert_s wants to minimize the final cost, so how much RP he costs by following the rule above?
Input
The first line contains two integer ;
The second line contains space-separated integers , the elements of the array.
Output
A number indicating the answer.
Sample Input
5 2
2 1 3 4 5
Sample Output
9
Solution
正好在做poj1064(卡精度了,我只差一点就被气死了),于是来做做这个
之前做二分的时候以为会一个函数就行了。。最近发现并不是这样的。
大概思路是:假定需要的天数k,枚举每天需要消耗的rp,判断是否满足k天(注意判断的时候不能等于k,因为无法控制当天是否满足枚举的rp),不断缩小范围,找到最大的一个。
AC代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;
int n,k;
int a[100005];
int tot=0;
bool pun(int s)
{
int sum=0;
int day=1;
for(int i=1;i<=n;++i)
{
sum=sum+a[i];
if(sum>s)
{
sum=a[i];
++day;
}
}
return day>k;
}
int main()
{
// int n,k;
scanf("%d %d",&n,&k);
int down=0;
for(int i=1;i<=n;++i)
{
scanf("%d",&a[i]);
tot=tot+a[i];
down=max(down,a[i]);
}
int up=tot;
//printf("%lld %lld\n",up,down);
while(up>down)
{
int mid=(up+down)/2;
// printf("%d\n",mid);
//printf("%d %d\n",up,down);
if(pun(mid)==1)
{
down=mid+1;
}
else up=mid;
}
printf("%d\n",up);
return 0;
}