-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaggcow.cpp
More file actions
60 lines (51 loc) · 1.1 KB
/
aggcow.cpp
File metadata and controls
60 lines (51 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
58
59
60
#include<iostream>
using namespace std;
#include<vector>
#include <algorithm>
bool isPossible(vector<int> &stalls, int k, int mid, int n) {
int cowCount = 1;
int lastPos = stalls[0];
for(int i=0; i<n; i++ ){
if(stalls[i]-lastPos >= mid){
cowCount++;
if(cowCount==k)
{
return true;
}
lastPos = stalls[i];
}
}
return false;
}
int aggressiveCows(vector<int> &stalls, int k)
{
sort(stalls.begin(), stalls.end());
int s = 0;
int n = stalls.size();
int e=stalls[n-1];
int ans = -1;
int mid = s + (e-s)/2;
while(s<=e) {
if(isPossible(stalls, k, mid, n)) {
ans = mid;
s = mid + 1;
}
else
{
e = mid - 1;
}
mid = s + (e-s)/2;
}
return ans;
}
int main() {
int n, k;
cin >> n >> k;
vector<int> stalls(n);
for(int i = 0; i < n; i++) {
cin >> stalls[i];
}
int result = aggressiveCows(stalls, k);
cout << result << endl;
return 0;
}