1331.Kick Veges' Ass SDNUOJ1331(简单二分)(2018总结赛)

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

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int N = 1e5;

int n, k;
int a[N + 5];

bool judge(int rp)
{
    int cnt = 1;
    int sum = 0;
    for(int i = 0; i < n; ++i)
    {
        sum += a[i];
        if(sum > rp)
        {
            sum = a[i];
            cnt++;
        }
    }
    return cnt <= k;
}

int main()
{
    while(~scanf("%d%d", &n, &k))
    {
        int l = 0;
        int r = 0;
        for(int i = 0; i< n; ++i)
        {
            scanf("%d", &a[i]);
            l = max(l, a[i]);
            r += a[i];
        }
        while(l < r)
        {
            int mid = (r + l) / 2;
//            cout << judge(mid) << '\n';
            if(judge(mid))///如果中值可行,要找更小的值就把它作为右界(上限)
                r = mid;
            else
                l = mid + 1;
        }
        cout << r << '\n';
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值