'commit'
This commit is contained in:
39
TangDou/LuoGuBook/P1824.cpp
Normal file
39
TangDou/LuoGuBook/P1824.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user