题目连接:https://www.nowcoder.com/acm/contest/106/K

       题意是有n块石头,然后给你了他们相邻石头之间的距离,然后问在不超过k步的情况下,他能跳的最大距离的最小值。这道题要用二分去做,二分的话就是求什么就去二分什么,所以这道题我们二分所跳的最大距离,然后判断这个值是否可行,可行的话再减小这个值,否则增大这个值。判断的话,如果二分的这个距离,小于其中一个相邻石头间的距离的话肯定是跳不过去的,否则的话就用ans来记录当前所能跳的最大值,当ans + pre[i] > x的时候说明这个石头跳不上去了,所以我们这一次就跳了sum的距离,然后以这个石头重新为起点。最后再判断所用的步数是否超过k就好了。


AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#define ll long long
using namespace std;
ll pre[100005];
ll n,c;

bool judge(ll x){
  ll ans = 0;
  ll num = 1;
  for(int i=0;i<n-1;i++){
    if(pre[i] > x){
      return false;
    }
    if(ans + pre[i] > x){
      ans = 0;
      num++;
    }
      ans += pre[i];
  }
  return num <= c;
}

int main()
{
  while(~scanf("%lld%lld",&n,&c)){
    for(int i=0;i<n-1;i++)scanf("%lld",&pre[i]);
    // sort(pre,pre+n-1);
    ll l = 0, r = 100000000050, mid;
    while(l < r){
      mid = (l + r) / 2;
      if(judge(mid)){
        r = mid;
      }
      else{
        l = mid + 1;
      }
    }
    printf("%lld\n",l);
  }
  return 0;
}