-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAGGRCOWS.cpp
More file actions
49 lines (45 loc) · 1.29 KB
/
AGGRCOWS.cpp
File metadata and controls
49 lines (45 loc) · 1.29 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
//http://www.spoj.com/problems/AGGRCOW/
#include <bits/stdc++.h>
#define ll long long
using namespace std;
bool check(vector<ll int>a , ll int m ,ll int n , ll int COWS)
{
ll int cows = 1 , prev = a[0];
for(int i = 1;i < n;i++)
{
if(a[i] - prev >= m)
{
prev = a[i];
cows++;
}
}
return (cows >= COWS);
}
int main()
{
std::ios::sync_with_stdio(0);
int T;cin >> T;
while(T--)
{
ll int n , c;
cin >> n >> c;
vector<ll int>a(n);
for(int i = 0;i < n;i++) cin >> a[i];
sort(a.begin() , a.end());
ll int low = 1 , high = a[n - 1] , mid , ans = 0;
while(low <= high)
{
mid = (low + high) / 2;//cout << mid << "\n";
bool ok = check(a , mid , n , c);
if(ok)
{
ans = max(ans , mid);
low = mid + 1;
}
else high = mid - 1;
}
cout << ans << "\n";
}
}
//tags : Binary Search , Implementation
//Complexity : O(n * log n)