https://www.cnblogs.com/violet-acmer/p/9793209.html
题意:
有 N 块岩石,从中去掉任意 M 块后,求相邻两块岩石最小距离最大是多少?
题解:
二分答案(假设答案为res)
定义 l = 0 , r = L ;
mid = (l+r)/2 ;
判断当前答案 mid 至少需要去除多少块岩石,如果去除的岩石个数 > M,说明当前答案mid > res,r=mid;反之,说明当前答案 mid <= res , l =mid;
AC代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=5e4+; int L,N,M;
int dist[maxn]; bool Check(int mid)
{
int res=;
int pre=;
for(int i=;i <= N;++i)
{
if(dist[i]-dist[pre] < mid)
res++;
else
pre=i;
}
return res <= M ? true:false;
}
int Solve()
{
sort(dist+,dist+N+);
int l=,r=L+;
while(r-l > )
{
int mid=l+((r-l)>>);
if(Check(mid))
l=mid;
else
r=mid;
}
return l;
}
int main()
{
scanf("%d%d%d",&L,&N,&M);
dist[]=;
for(int i=;i <= N;++i)
scanf("%d",dist+i);
printf("%d\n",Solve());
return ;
}