题解:使用二分搜索答案即可
附上代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e5+50;
int x[maxn];
int n,m;
bool C(int d)
{
int last=0;
for(int i=1;i<m;i++){
int crt=last+1;
while(crt<n&&x[crt]-x[last]<d){
crt++;
}
if(crt==n){
return false;
}
last=crt;
}
return true;
}
int main()
{
scanf("%d%d",&n,&m);
int inf=0;
for(int i=0;i<n;i++){
scanf("%d",&x[i]);
inf=max(x[i],inf);
}
sort(x,x+n);
int lb=0,ub=inf;
while(ub-lb>1){
int mid=(lb+ub)/2;
if(C(mid)){
lb=mid;
}else{
ub=mid;
}
}
printf("%d\n",lb);
return 0;
}