PS: 最小值最大化,二分。省赛热身。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
int n, m;
vector<int> v;
int maxv;
bool ok(int d) {
int last = 0, cur = last+1;
for(int i = 1; i < m; i++) {
while(cur<n && (v[cur]-v[last]<d)) {
cur++;
}
if(cur>=n) return false;
last = cur;
}
return true;
}
int work() {
sort(v.begin(), v.end());
int l = 0, r = maxv;
int mid;
while(r-l>1) {
mid = (l+r)/2;
if(ok(mid)) l = mid;
else r = mid;
}
return l;
}
int main()
{
int x;
while(scanf("%d%d", &n, &m)!=EOF) {
v.clear();
maxv = 0;
for(int i = 0; i < n; i++) {
scanf("%d", &x);
v.push_back(x);
maxv = max(maxv, x);
}
int res = work();
printf("%d\n", res);
}
return 0;
}