-
Notifications
You must be signed in to change notification settings - Fork 0
/
Aggrcow.cpp
39 lines (39 loc) · 1015 Bytes
/
Aggrcow.cpp
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
// Ivan Carvalho
// Solution to https://www.spoj.com/problems/AGGRCOW/
#include <algorithm>
#include <cstdio>
#define MAXN 100001
using namespace std;
int posicao[MAXN], n, c;
int valido(int menor) {
int vacas = 1, ultima = posicao[1];
for (int i = 2; i <= n; i++) {
if (posicao[i] - ultima >= menor) {
vacas++;
if (vacas == c) return 1;
ultima = posicao[i];
}
}
return 0;
}
int main() {
int casos;
scanf("%d", &casos);
while (casos--) {
scanf("%d %d", &n, &c);
for (int i = 1; i <= n; i++) scanf("%d", &posicao[i]);
sort(posicao + 1, posicao + n + 1);
int ini = posicao[1], fim = posicao[n], meio, resposta;
while (ini <= fim) {
meio = (ini + fim) / 2;
if (valido(meio)) {
resposta = meio;
ini = meio + 1;
} else {
fim = meio - 1;
}
}
printf("%d\n", resposta);
}
return 0;
}