输入样例
5 3
1
2
8
4
9
输出样例
3
其实不难,普普通通二分查找。//但智障如我,数组开小了,还忘记注释重定向...
AC代码
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int a[110000]; int n, m;
int isok(int d){
int cnt = 0, tot = 1;
for(int i = 1; i < n; ++i){
if(a[i] - a[cnt] >= d){
cnt = i, ++tot;
if(tot >= m) return 1;
}
}
return 0;
}
int main(){
cin >> n >> m;
for(int i = 0; i < n; ++i) scanf("%d", &a[i]);
sort(a, a+n);
int lef = 1, righ = a[n-1], mid = lef + (righ-lef)/2;
while(righ - lef > 1){
mid = lef + (righ-lef) / 2;
if(isok(mid)) lef = mid;
else righ = mid;
}
cout << lef;
return 0;
}