P2678 跳石头

题解:二分枚举答最长的最短跳跃距离。本题关键在于洛谷P2678 跳石头(二分枚举)_#include函数怎么写。我们可以这样想,如果当前石头与上一块石头的距离小于枚举的答案,那么说明当前石头需要被移走,依次反复,如果到达终点的时候所移走的石头不超过洛谷P2678 跳石头(二分枚举)_#ifndef_02块,那么说明当前枚举的答案可行,但是需要继续增加距离,因为可能还不是最长的,否则就减小距离。

代码

#include<bits/stdc++.h>
typedef long long LL;
using namespace std;

int L,N,M,a[100010],vis;

bool ok(int x)
{
int ret = 0, sx = 0;
for(int i = 1 ; i <= N+1; ++i){
if(a[i] - a[sx] < x)
ret++;
else
sx = i;
}
return ret <= M;
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("input.in","r",stdin);
#endif
cin>>L>>N>>M;
for(int i = 1; i <= N; ++i)
cin>>a[i];
a[N+1] = L;
int j = L, i = 1;
while(i <= j){
int mid = (i+j)/2;
if(ok(mid))
i = mid+1;
else
j = mid-1;
}
cout<<i-1<<endl;
return 0;
}