-
Notifications
You must be signed in to change notification settings - Fork 1
/
aggrcw.cpp
69 lines (45 loc) · 865 Bytes
/
aggrcw.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
//binary search
//aggressive cows
#include <bits/stdc++.h>
using namespace std;
#define ll long
bool canPlaceCows(int *shelter, int n, int cows, int min_step){
int lastCow = shelter[0];
int cnt=1;
for(int i=1;i<n;i++){
if(shelter[i]-lastCow>=min_step){
lastCow = shelter[i];
cnt++;
if(cnt==cows){
return true;
}
}
}
return false;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("inputs.txt","r",stdin);
freopen("outputs.txt","w",stdout);
#endif
int shelter[] = {1,2,4,8,9};
int cows = 3;
int n=5;
//binary search algorithm
int s=0;
int e= shelter[n-1] - shelter[0];
int ans=0;
while(s<=e){
int mid = (s+e)/2;
bool keepCow = canPlaceCows(shelter,n,cows,mid);
if(keepCow){
ans = mid;
s= mid+1;
}
else e = mid-1;
}
cout<<ans;
// aggrcw(shelter, n , cows);
return 0;
}