-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAGGRCOW.cpp
55 lines (47 loc) · 812 Bytes
/
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
52
53
54
55
// AggressiveCows by github.com/shubham-khanna
#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long int
bool KyaPlaceHoPaai(ll arr[],ll n,ll cows,ll dist){
ll placed_cows=1;
ll pos=arr[0];
for(ll i=1;i<n;i++){
if(arr[i]-pos>=dist){
placed_cows++;
pos=arr[i];
if(placed_cows==cows){
return true;
}
}
}
return placed_cows==cows;
}
int AggressiveCows(ll arr[],ll n,ll cows){
ll s=0;
ll e=arr[n-1];
ll ans=0;
while(s<=e){
ll mid=(s+e)/2;
if(KyaPlaceHoPaai(arr,n,cows,mid)){
ans=max(ans,mid);
s=mid+1;
}
else{
e=mid-1;
}
}
return ans;
}
int main(){
ll t,n,cows;
cin>>t;
ll arr[1000001];
while(t--){
cin>>n>>cows;
for(ll i=0;i<n;i++){cin>>arr[i];}
sort(arr,arr+n);
cout<<AggressiveCows(arr,n,cows)<<endl;
}
return 0;
}