本题与poj2456非常相似,只需更改几个条件
使用二分搜索加快速度
poj 2456:https://blog.csdn.net/qq_43768149/article/details/86754555
#include <cstdio>
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>
#include <ctime>
#include <complex>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll mod=1e9+7;
const ll MOD=1e9+7;
const ll inf = 0x3f3f3f3f;
const int maxn = 100005;
const int maxa = 300005;
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
ll n,m,l;
ll str[50005];
bool C(int x)
{
int last=0;
for(int i=0;i<n-m;i++)
{
int cur=last+1;
while(cur<=n&&str[cur]-str[last]<x)
{
cur++;
}
if(cur>n)
return 0;
last=cur;
}
return 1;
}
void solve()//二分
{
ll l=0,r=inf;
while(r-l>1)
{
ll mid=(l+r)/2;
if(C(mid)) l=mid;
else
r=mid;
}
cout<<l<<endl;
}
int main()
{
cin>>l>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>str[i];
}
if(m==n)
{
cout<<l<<endl;
return 0;
}
str[n+1]=l;
sort(str+1,str+n+1);
solve();
return 0;
}