The problem asks for the largest value such that of the outposts can be chosen with every pair of chosen outposts at distance at least . Searching over selections directly is hopeless — there are \binom{n}{k} of them — but searching over the answer is easy, because deciding whether a fixed is achievable turns out to be a linear-time greedy.
The property that makes this work is monotonicity. If relays can be placed so that all pairwise distances are at least , then that same placement also satisfies the requirement for every d' < d. So the feasible values form a prefix 1, 2, \dots, D of the integers, and the answer is exactly the last feasible value . That is precisely the shape a binary search needs.
Fix a candidate and ask: can we choose at least outposts with consecutive chosen coordinates differing by at least ? Because the outposts lie on a line and are given in increasing order, only consecutive chosen outposts matter — if neighbours in the chosen set are at least apart, every other pair is too.
Greedy: take x_1, then walk left to right and take an outpost whenever it is at least away from the last one taken. Let be the number of outposts this selects; is feasible if and only if c(d) \ge k.
Why the greedy is optimal is an exchange argument. Suppose some valid selection of size uses positions p_1 < p_2 < \dots < p_m, and let g_1 < g_2 < \dots be the greedy's choices. By induction g_i \le p_i for every : it holds for i = 1 since g_1 = x_1 is the leftmost outpost, and if g_i \le p_i then p_{i+1} \ge p_i + d \ge g_i + d, so an outpost at distance from g_i exists no later than p_{i+1} — and the greedy takes the first such one, hence g_{i+1} \le p_{i+1}. The greedy therefore never falls behind, and it selects at least as many outposts as any valid selection.
The answer lies in [1, (x_n - x_1)/(k-1)]: the coordinates are distinct integers so distance is always achievable, and the chosen outposts span at most x_n - x_1 with k-1 gaps of size at least each, giving
Binary search on this range, running the greedy at each step, and keep the largest with c(d) \ge k.
#include <bits/stdc++.h>
using namespace std;
int n, k;
vector<int> x;
// how many outposts the greedy selects with minimum spacing d
int count_relays(int d) {
int cnt = 1, last = x[0];
for (int i = 1; i < n; i++) {
if (x[i] - last >= d) {
cnt++;
last = x[i];
}
}
return cnt;
}
int main() {
scanf("%d %d", &n, &k);
x.resize(n);
for (int i = 0; i < n; i++) scanf("%d", &x[i]);
int lo = 1, hi = (x[n-1] - x[0]) / (k - 1), ans = 0;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (count_relays(mid) >= k) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
printf("%d\n", ans);
return 0;
}Two boundary cases are worth checking against the code. When k = n every outpost is used and the answer is the smallest adjacent gap \min_i (x_{i+1} - x_i) — the greedy takes everything for exactly that and misses one outpost for anything larger. When k = 2 the answer is x_n - x_1, which is also the upper bound of the search range, so the search returns it immediately. The division by k-1 is safe because k \ge 2.
No overflow is possible: coordinates are at most 10^9 and every quantity computed is a difference of two coordinates, so 32-bit signed arithmetic suffices.
Complexity analysis:
Time complexity: O(n \log X), where X = x_n - x_1 \le 10^9 — about greedy passes over the array
Space complexity: for the coordinates