二分搜索,在二分搜索调用的函数上花了很多时间
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int M=500005;
int st[M];
bool isj(int mi,int n,int m) //能不能跳过去
{
int p=0,i=1;
int co=0;
while(i<=n+1)
{
//cout<<"st"<<i<<" "<<p<<endl;
if(st[i]-st[p]<=mi) //符合跳mi时最多能调到
{
i++;
}
else
{
if(i-p==1) //有相邻的一跳mi距离不够
return 0;
else //不是相邻的说明前面的满足条件,起跳点提前
{
p=i-1;
co++;
}
}
}
return co+1<=m;
}
int bs(int l,int n,int m)
{
int lo=0,hi=l;
int mid,re=l;
while(lo<=hi)
{
mid=lo+(hi-lo)/2;
//cout<<mid<<" "<<isj(mid,n,m)<<endl;
if(!isj(mid,n,m))
lo=mid+1;
else
{
hi=mid-1;
re=min(re,mid); //需要记录答案,因为光满足跳跃数目不够
}
}
return re;
}
int main()
{
int l,n,m;
while(scanf("%d%d%d",&l,&n,&m)!=EOF)
{
for(int i=1;i<=n;i++)
scanf("%d",&st[i]);
st[0]=0;
st[n+1]=l;
sort(st,st+n+2);
printf("%d\n",bs(l,n,m));
}
return 0;
}