-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaggrCow.cpp
76 lines (50 loc) · 1.2 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <algorithm>
using namespace std;
bool cowsKaHoPayega(int *stalls,int stall, int noOfCows,int minDistance) {
int temp = stalls[0];
int count = 1;
for(int i = 1; i < stall; i++) {
if(stalls[i]- temp >= minDistance) {
count++;
temp = stalls[i];
if(count == noOfCows) return true;
}
}
return false;
}
int findMinimumDistance(int *stalls,int stall, int noOfCows) {
sort(stalls,stalls+stall);
int start = 0;
int end = stalls[stall-1] - stalls[0];
int cowsKaMinimumDistance = 0;
while(start<=end) {
int mid = (start+end)>>1;
if(cowsKaHoPayega(stalls,stall,noOfCows,mid)) {
cowsKaMinimumDistance = mid;
start = mid+1;
} else {
end = mid - 1;
}
}
return cowsKaMinimumDistance;
}
int main() {
#ifndef ONLINE_JUDGE
// for getting input from input.txt
freopen("input.txt", "r", stdin);
// for writing output to output.txt
freopen("output.txt", "w", stdout);
#endif
int t;
cin>>t;
while(t--) {
long long int stall, noOfCows;
cin>>stall>>noOfCows;
int *stalls = new int[stall];
for(int i=0; i <stall;i++) {
cin>>stalls[i];
}
cout<<findMinimumDistance(stalls,stall,noOfCows)<<endl;
}
}