-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggressivecows.cpp
45 lines (44 loc) · 1.06 KB
/
aggressivecows.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
#include <iostream>
#include <cmath>
#include <stack>
#include <cstring>
#include <stdlib.h>
#include <unordered_set>
#include <vector>
#include <map>
#include <numeric>
#include <set>
#include <unordered_map>
#include <algorithm>
#include <queue>
#include <utility>
#include <climits>
#define vi vector<int>
#define vvi vector<vi>
#define pii vector<int, int>
#define vii vector<pii>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define ff first
#define ss second
using namespace std;
bool ispossible(vi& stalls,int dist, int cows){
int cntcows=1, last=stalls[0];
for(int i=1;i<stalls.size();i++){
if(stalls[i]-last>=dist){
cntcows++;
last=stalls[i];
}
if(cntcows>=cows)return true;
}
return false;
}
int maxdis(vi &stalls, int n, int k){
sort(stalls.begin(),stalls.end());
int low=1, high=stalls[n-1]-stalls[0];
while(low<=high){
int mid=(low+high)/2;
if(ispossible(stalls,mid,k))low=mid+1;
else high=mid-1;
}
return high;
}