-
Notifications
You must be signed in to change notification settings - Fork 1
/
q1.cpp
70 lines (57 loc) · 1.21 KB
/
q1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <vector>
#include <algorithm>
using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::sort;
// 1:Aggressive cows
// 约翰在他的畜棚上建立了N(2<=N<=100000)个畜栏
// 畜栏分布在一条直线上,x1...xN(0<=xi<=1000000000)
// 奶牛放在畜栏里,最大化它们间的最小间隔,并输出这个值
vector<int> pos;
// 极值问题->状态判断
bool check(int space, int cowNum) {
// 第一个畜栏放入奶牛
int pre = pos[0];
cowNum--;
int length = pos.size();
for (int i = 1; i < length; i++) {
if (pos[i] - pre >= space) {
// 间距足够,当前畜栏放入奶牛
pre = pos[i];
cowNum--;
}
}
if (cowNum <= 0) {
return true;
} else {
return false;
}
}
int main(int argc, char *argv[]) {
int n = 0;
int c = 0;
cin >> n >> c;
for (int i = 0; i < n; i++) {
int k = 0;
cin >> k;
pos.push_back(k);
}
sort(pos.begin(), pos.end());
int left = 0;
int right = pos[n - 1] - pos[0];
int ans = right;
while (left <= right) {
int middle = left + (right - left) / 2;
if (check(middle, c)) {
ans = middle;
left = middle + 1;
} else {
right = middle - 1;
}
}
cout << ans << endl;
return 0;
}