第十四届华中科技大学程序设计竞赛 K-Walking in the Forest 【二分】

题目描述 
It’s universally acknowledged that there’re innumerable trees in the campus of HUST.


Now you're going to walk through a large forest. There is a path consisting of N stones winding its way to the other side of the forest. Between every two stones there is a distance. Let di indicates the distance between the stone i and i+1.Initially you stand at the first stone, and your target is the N-th stone. You must stand in a stone all the time, and you can stride over arbitrary number of stones in one step. If you stepped from the stone i to the stone j, you stride a span of (di+di+1+...+dj-1). But there is a limitation. You're so tired that you want to walk through the forest in no more than K steps. And to walk more comfortably, you have to minimize the distance of largest step.
输入描述:
The first line contains two integer N and K as described above.
Then the next line N-1 positive integer followed, indicating the distance between two adjacent stone.
输出描述:
An integer, the minimum distance of the largest step.
示例1
输入


6 3
1 3 2 2 5
输出


5

大写的门

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int MAX = 1e5 + 7;
ll a[MAX];
ll s, mx = 0, sum = 0, n, k;
void Bsearch()
{
    ll l = mx, r = sum;
    ll ans;
    while(l <= r)
    {
        ll mid = (l + r) >> 1;
        s = 0;
        ll S = 0;
        for(int i = 1; i <= n - 1; i++)
        {
            s += a[i];
            if(s > mid)
            {
                s = a[i];
                S++;
            }
        }
        if(S >= k)
            l = mid + 1;
        else
        {
            r = mid - 1;
            ans = mid;
        }
    }
    printf("%lld\n", ans);
}
int main()
{
    scanf("%lld%lld", &n, &k);
    for(int i = 1; i <= n - 1; i++)
    {
        scanf("%lld", &a[i]);
        mx = max(mx, a[i]);
        sum += a[i];
    }
    Bsearch();
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值