-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathaggresive_cows.cpp
More file actions
58 lines (42 loc) · 1.1 KB
/
Copy pathaggresive_cows.cpp
File metadata and controls
58 lines (42 loc) · 1.1 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
56
57
//Problem Link : https://www.spoj.com/problems/AGGRCOW/
//Solution :
#include<bits/stdc++.h>
using namespace std;
bool bs(long long * arr , int c, int n, long long dis){
int count = 1;
long long last_position = arr[0];
for(int i = 1; i < n ; i++){
if(arr[i] - last_position >= dis){
count++;
last_position = arr[i];
}
if(count == c)
return true;
}
return false;
}
int main() {
int t;
cin>>t;
while(t--){
int n,c;
cin>>n>>c;
long long *arr = new long long[n];
for(int i = 0 ; i < n ; i++)
cin>>arr[i];
sort(arr,arr+n);
long long ans = -1;
long long start = 0;
long long end = arr[n-1] - arr[0];
while(start <= end){
long long mid = start + (end - start)/2;
if(bs(arr,c,n,mid)){
ans = mid ;
start = mid +1;
}
else
end = mid -1;
}
cout<<ans<<endl;
}
}