-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAggressive_Cows.cpp
More file actions
49 lines (45 loc) · 871 Bytes
/
Aggressive_Cows.cpp
File metadata and controls
49 lines (45 loc) · 871 Bytes
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
/*
* @Date : 2020-04-30 11:14:33
* @Author : Abhimanyu Kumar Maurya (aerma7309@gmail.com)
*/
#include <bits/stdc++.h>
using namespace std;
bool ib = ios_base::sync_with_stdio(0);
bool it = cin.tie(0);
#define int long long
bool IsValid(vector<int> &v, int c, int ans)
{
int cnt = 0, ptr = 0;
for (int i : v)
{
if (i >= ptr)
{
cnt++;
ptr = i + ans;
}
}
return (cnt >= c);
}
signed main()
{
int n, c;
cin >> n >> c;
vector<int> v(n);
for (int &i : v)
cin >> i;
sort(v.begin(), v.end());
int lb = 0, ub = n, mid, res = -1;
while (lb <= ub)
{
mid = (lb + ub) / 2;
if (IsValid(v, c, mid))
{
res = mid;
lb = mid + 1;
}
else
ub = mid - 1;
}
cout << res << '\n';
return 0;
}