二分搜答案。
这是一个最大最小值问题,用二分找到一个数字,表示最小距离,带入到石头里面,看能否满足至少有m-n个石头块能够满足最小距离x。不断二分去找到最大值。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<map>
#define up(i,a,b) for(int i=a;i<b;i++)
#define dw(i,a,b) for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
const double esp = 1e-6;
const double pi = acos(-1.0);
const long long INF = 0x3f3f3f3f;
using namespace std;
typedef pair<int, int> pir;
int l, m, n;
int a[500004];
bool judge(int x)
{
int cnt = 0;
int temp;
upd(i,0,n-m)//至少n-m个石头块
{
temp = cnt + 1;
while (temp < n+2&&a[temp] - a[cnt]<x)
temp++;
if (temp == n + 2)return false;
cnt = temp;
}
return true;
}
int main()
{
cin >> l >> n >> m;
upd(i, 1, n)
{
cin >> a[i];
}
a[0] = 0;
a[n + 1] = l;
sort(a, a + n + 2);//需要先排序
int lf = 0;
int rt = l+2;
int mid = 0;
while (rt - lf > 1)//左闭右开区间取决于if条件的判断
{
mid = (lf + rt) >> 1;
if (judge(mid))lf = mid;
else rt = mid;
}
cout << lf;
return 0;
}