SDNU1331 Kick Veges' Ass(二分)

7 篇文章 0 订阅

技改?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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值