forked from Rishu1018/HackertoberFest2022-Repostory_Newbie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AggressiveCows.cpp
47 lines (44 loc) · 972 Bytes
/
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
46
47
#include <iostream>
#include <climits>
#include <algorithm>
using namespace std;
#define ll long long
bool KyaPlaceHui(ll *a, ll n, ll c, ll mid) {
ll placed_cows = 1;
ll placed_cow_pos = 0;
for (ll i = 1; i < n; ++i)
{
if (a[i] - a[placed_cow_pos] >= mid) {
placed_cows++;
placed_cow_pos = i;
if (placed_cows == c) {
return true;
}
}
}
return false;
}
ll AggressiveCows(ll *a, ll n, ll c) {
ll s = 0, e = INT_MAX;
ll ans = INT_MIN;
while (s <= e) {
ll mid = (s + e) / 2;
if (KyaPlaceHui(a, n, c, mid)) {
ans = max(ans, mid);
s = mid + 1;
}
else {
e = mid - 1;
}
}
return ans;
}
int main() {
ll n, c;
ll a[100005];
cin >> n >> c;
for (ll i = 0; i < n; ++i) {cin >> a[i];}
sort(a, a + n);
cout << AggressiveCows(a, n, c) << endl;
return 0;
}