You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
957 B
39 lines
957 B
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
typedef long long LL;
|
|
|
|
const int N = 1e5 + 10;
|
|
int n, c;
|
|
int a[N];//表示每个隔间的坐标
|
|
|
|
bool check(int len) {
|
|
//如果间距长度为 len,计算能安排下几头奶牛
|
|
int now = a[1];
|
|
int cnt = 1;//因为第一个位置需要先放一个
|
|
for (int i = 2; i <= n; i++)
|
|
if (a[i] - now >= len) {
|
|
cnt++;
|
|
now = a[i];
|
|
}
|
|
//如果能安排下的奶牛数量大于c,就是合理的,否则就是不合理的
|
|
return cnt >= c;
|
|
}
|
|
|
|
int main() {
|
|
//输入
|
|
cin >> n >> c;
|
|
for (int i = 1; i <= n; i++) cin >> a[i];
|
|
//排序
|
|
sort(a + 1, a + n + 1);
|
|
|
|
//左右边界
|
|
int l = a[1], r = a[n];
|
|
while (l < r) {
|
|
int mid = l + r + 1 >> 1;
|
|
if (check(mid)) l = mid;//距离尽可能的大,就是向右逼近
|
|
else r = mid - 1;
|
|
}
|
|
cout << l << endl;
|
|
return 0;
|
|
} |