-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaggressiveCows.cpp
More file actions
55 lines (42 loc) · 1.09 KB
/
aggressiveCows.cpp
File metadata and controls
55 lines (42 loc) · 1.09 KB
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
#include<bits/stdc++.h>
using namespace std;
int main() {
// Write your code here
int t;
cin >> t;
while(t--){
int n, c;
cin >> n >> c;
int *pos = new int[n];
for(int i = 0; i < n; i++){
cin >> pos[i];
}
sort(pos, pos + n);
int minDist = 0, maxDist = pos[n-1] - pos[0];
int distance = 0;
int ans = -1;
while(minDist <= maxDist){
distance = (minDist + maxDist)/2;
int remCows = c-1;
int i = 1;
int j = 0;
while(remCows > 0 && i < n){
if((pos[i] - pos[j]) >= distance){
remCows--;
j = i;
}
i++;
}
if(remCows > 0){
maxDist = distance - 1;
}
else{
minDist = distance + 1;
ans = distance;
}
// cout << ans << endl;
}
cout << ans << endl;
delete [] pos;
}
}