#include <bits/stdc++.h>
using namespace std;
/*
QUESTION IN SIMPLE WORDS
------------------------
Some stations are already placed on a straight line.
We are allowed to add exactly K new repeaters at integer positions.
After adding them, we want the largest gap between any two consecutive
stations to be as small as possible.
Goal:
Return the minimum possible value of that largest gap.
------------------------------------------------------------
BRUTE FORCE IDEA
------------------------------------------------------------
Try every possible way to place K repeaters.
For each placement:
- sort all station positions
- compute all consecutive gaps
- take the maximum gap
- keep the best answer
------------------------------------------------------------
WHY BRUTE FORCE IS TOO SLOW
------------------------------------------------------------
There are too many possible integer positions.
The number of placements becomes huge very quickly.
------------------------------------------------------------
OPTIMIZED IDEA
------------------------------------------------------------
This is a "minimize the maximum" problem.
That pattern usually suggests:
binary search on the answer.
Suppose we guess the answer = gapLimit.
Then we can check:
How many repeaters are needed so that every original gap becomes <= gapLimit?
If needed repeaters <= K, then this gapLimit is possible.
So we try smaller.
Otherwise, we need a bigger answer.
*/
long long minimumLargestGap(vector<int> pos, int k) {
sort(pos.begin(), pos.end());
if ((int)pos.size() <= 1) return 0;
// Returns how many extra repeaters we need
// if maximum allowed gap is gapLimit.
auto need = [&](long long gapLimit) -> long long {
long long extra = 0;
for (int i = 1; i < (int)pos.size(); i++) {
long long gap = pos[i] - pos[i - 1];
/*
Example:
positions: 1 and 10
gap = 9
If gapLimit = 3,
we need 2 extra repeaters to split 9 into 3,3,3.
Formula:
(gap - 1) / gapLimit
*/
extra += (gap - 1) / gapLimit;
}
return extra;
};
long long lo = 1;
long long hi = 0;
// Maximum original gap is a safe upper bound.
for (int i = 1; i < (int)pos.size(); i++) {
hi = max(hi, 1LL * pos[i] - pos[i - 1]);
}
// Binary search smallest feasible answer
while (lo < hi) {
long long mid = (lo + hi) / 2;
if (need(mid) <= k) {
hi = mid;
} else {
lo = mid + 1;
}
}
return lo;
}
int main() {
vector<int> pos = {1, 10};
int k = 2;
cout << minimumLargestGap(pos, k) << '\n';
return 0;
}