forked from pranavanurag/SPOJSolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAGGRCOW.cpp
More file actions
48 lines (43 loc) · 713 Bytes
/
Copy pathAGGRCOW.cpp
File metadata and controls
48 lines (43 loc) · 713 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
#include <bits/stdc++.h>
using namespace std;
int N, A[1000001], T, C;
bool Predicate(int MinDist)
{
int Placed = 1, LastPlacedAt = A[1];
for (int i = 2; i <= N && Placed < C; i++)
{
if (A[i] - LastPlacedAt >= MinDist)
{
LastPlacedAt = A[i];
Placed++;
}
}
return (Placed == C);
}
int BinSearch(int Low, int High)
{
int Mid = Low + (High - Low + 1)/2;
while (Low < High)
{
if (Predicate(Mid))
Low = Mid;
else
High = Mid - 1;
Mid = Low + (High - Low + 1)/2;
}
return High;
}
int main()
{
ios::sync_with_stdio(false);
cin>>T;
while (T--)
{
cin>>N>>C;
for (int i = 1; i <= N; i++)
cin>>A[i];
sort(A + 1, A + N + 1);
cout<<BinSearch(1, A[N])<<endl;
}
return 0;
}