SDNU 1331.Kick Veges' Ass(二分法)

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

Hint

Another vege is waiting to be kicked.
 
End:
 
#include <cstdio>
#include <iostream>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

#define ll long long

const int maxn = 1e5+100;
int a[maxn];
int n, m;

bool c(ll x)
{
    ll cnt = 1, sum = 0;
    for(int i = 0; i<n;)
    {
        if(x >= sum+a[i])//用二分的方法来不断把菜的屁股一段一段踢掉
        {
            sum += a[i];
//            printf("i = %d, sum = %lld\n",i,  sum);
            i++;
        }
        else
        {
            sum = 0;//那一天不能踢那么多屁股,就从新的一天开始
            cnt++;//统计天数目
//            printf("sum = %lld, cnt = %lld\n", sum, cnt);
            if(cnt>m)return 0;//如果天数大于输入的天数,则返回0
        }
    }
    return cnt <= m;//必须要二分的结果小于输入的天数
}
int main()
{
    scanf("%d%d", &n, &m);
    for(int i = 0; i<n; i++)
    {
        scanf("%d", &a[i]);
    }
    ll lb = 0, ub = 1e14;
//    printf("lb = %lld, ub = %lld\n", lb, ub);
    while(ub>lb)
    {
        ll mid  = (lb+ub)/2;
        if(c(mid))ub = mid;
        else lb = mid +1;//之前的一个已经遍历过了,从下一个开始
//        printf("lb = %lld, ub = %lld\n", lb, ub);
    }
    printf("%lld\n", ub);
    return 0;
}

 

转载于:https://www.cnblogs.com/RootVount/p/10427965.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值