-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAggressive Cows.cpp
73 lines (60 loc) · 1.12 KB
/
Aggressive Cows.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
71
72
73
//Time O(nlogd) Space O(1)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool check(int c, ll arr[], int n, ll d ){
ll last_position = arr[0];
int count = 1;
for(int i=1;i<n;i++){
if((arr[i] - last_position) >= d){
last_position = arr[i];
count++;
}
if(count == c){
return true;
}
}
return false;
}
int main() {
// your code goes here
int t;
cin >> t;
while(t--){
int n,c;
cin >> n >> c;
ll arr[n];
for(int i=0;i<n;i++){
cin >> arr[i];
}
sort(arr,arr+n);
ll start = 0;
ll end = arr[n-1] - arr[0];
ll ans = 0;
while(start <= end){
ll mid = start + (end - start)/2;
if(check(c,arr,n,mid)){
ans = mid;
start = mid + 1;
}
else{
end = mid - 1;
}
}
cout << ans << endl;
}
return 0;
}
SAMPLE INPUT:
1
5 3
1
2
8
4
9
SAMPLE OUTPUT:
3
EXPLANATION:
FJ can put his 3 cows in the stalls at positions 1, 4 and 8,
resulting in a minimum distance of 3.