-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathAGGRCOW.cpp
51 lines (45 loc) · 1.17 KB
/
AGGRCOW.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
// https://www.spoj.com/problems/AGGRCOW/
#include <bits/stdc++.h>
using namespace std;
int n, c;
// method to check if we can put all cows in the stalls such that
// the minimum distance between any two cows is greater or equal to dist
bool checkDist(int dist, vector<int>& stall) {
int atDist = stall[0];
int atC = c-1;
for(int i = 1; i < n; i++) {
if(stall[i] - atDist >= dist) {
atDist = stall[i];
atC--;
}
if(atC <= 0) return true;
}
return false;
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);
int test;
cin >> test;
for(int t = 0; t < test; t++) {
cin >> n >> c;
// read input and sort vector
vector<int> stall(n);
for(int i = 0; i < n; i++) cin >> stall[i];
sort(stall.begin(), stall.end());
// binary search the answer
int lo = 0;
int high = 1e9+1;
while(high - lo > 1) {
int mid = lo + (high - lo) / 2;
if(checkDist(mid, stall)) {
lo = mid;
}
else {
high = mid;
}
}
cout << lo << '\n';
}
return 0;
}