二分,关键是转化为二分!
#include <cstdio>
#include <algorithm>
const int maxn = ;
const int maxN = ; int N, C;
int a[maxN];
using namespace std;
bool judge(int x) {
int s = ;
for (int i = ; i < C;i++) {
int ctr = s+;
while (ctr < N && (a[ctr]-a[s] < x)) {
ctr++;
}
if (ctr==N) {
return false;
}
s = ctr;
}
return true;
}
int main(void)
{
scanf("%d%d", &N, &C);
for (int i = ; i < N; i++) {
scanf("%d", &a[i]);
}
sort(a, a+N);
int l = , u = maxn, mid;
for (;(u-l)>;){
mid = (l+u)/;
if (judge(mid)) {
l = mid;
} else {
u = mid;
}
}
printf("%d\n", l);
return ;
}