-
Notifications
You must be signed in to change notification settings - Fork 0
/
AGGRCOW.java
56 lines (52 loc) · 1.3 KB
/
AGGRCOW.java
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
50
51
52
53
54
55
56
/**
* https://www.spoj.com/problems/AGGRCOW/ tag: #binary-search minimum distance between two position
* in a range.
*/
import java.util.Arrays;
import java.util.Scanner;
class AGGRCOW {
static int getResult(int[] source, int countPos) {
Arrays.sort(source);
int startVal = 0;
int endVal = source[source.length - 1] - source[0];
int ans = 0;
int midVal;
while (startVal <= endVal) {
midVal = startVal + (endVal - startVal) / 2;
int prev = source[0];
int count = 0;
for (int i = 1; i < source.length; i++) {
if (source[i] - prev >= midVal) {
count++;
prev = source[i];
}
}
if (count >= countPos - 1) {
if (midVal > ans) {
ans = midVal;
}
startVal = midVal + 1;
} else {
endVal = midVal - 1;
}
}
return ans;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testcases = sc.nextInt();
int n, c;
for (int t = 0; t < testcases; t++) {
n = sc.nextInt();
c = sc.nextInt();
int[] source = new int[n];
int tmp;
for (int i = 0; i < n; i++) {
tmp = sc.nextInt();
source[i] = tmp;
}
int result = getResult(source, c);
System.out.println(result);
}
}
}