-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspojAGGRCOW.cpp
More file actions
59 lines (43 loc) · 817 Bytes
/
Copy pathspojAGGRCOW.cpp
File metadata and controls
59 lines (43 loc) · 817 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
50
51
52
53
54
55
56
57
58
59
/*
SPOJ - Aggressive cows
https://www.spoj.com/problems/AGGRCOW/
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fastio ios::sync_with_stdio(0); cin.tie(0);
const int MAX = 1e5;
int tc, n, k, v[MAX];
int f(int x) {
int cnt = 1, prev = v[0];
for (int i = 1; i < n; i++) {
if (v[i] - prev >= x) {
prev = v[i];
cnt++;
}
}
return cnt;
}
int bs() {
int l = 0, r = v[n - 1] - v[0], m, ans = 0;
while (l <= r) {
m = l + (r - l) / 2;
if (f(m) >= k) {
ans = m;
l = m + 1;
} else
r = m - 1;
}
return ans;
}
int main() {
fastio
scanf("%i", &tc);
while (tc--) {
scanf("%i %i", &n, &k);
for (int i = 0; i < n; i++) scanf("%i", &v[i]);
sort(v, v + n);
printf("%i\n", bs());
}
return 0;
}