E - 街灯の配置 / Placement of Street Lights Editorial by admin Editorial for printing
gpt-5.5-highOverview
We can binary search for the minimum value \(x\) such that the maximum danger level of each section can be at most \(x\).
The decision problem can be rephrased as an interval stabbing problem: “Can we place points (streetlights) such that there is at least one point within the range required by each section?”
Analysis
Let us fix a value \(x\) and consider the following:
Is it possible to make the final danger level of all sections at most \(x\)?
If no streetlight is placed in section \(i\), its final danger level is:
\(A_i \times d_i\)
To make this at most \(x\), we must have:
\(A_i \times d_i \leq x\)
When \(A_i > 0\), this is equivalent to:
\(d_i \leq \left\lfloor \frac{x}{A_i} \right\rfloor\)
In other words, there must be at least one streetlight within a distance of \(\left\lfloor \frac{x}{A_i} \right\rfloor\) from section \(i\).
Therefore, section \(i\) requires a streetlight to be placed in the following interval:
\([L_i, R_i] = \left[i - \left\lfloor \frac{x}{A_i} \right\rfloor,\ i + \left\lfloor \frac{x}{A_i} \right\rfloor\right]\)
Note that we cannot place streetlights outside the road, so we clip the interval to be within \([1, N]\).
Now, the problem becomes:
What is the minimum number of points required to ensure that every interval \([L_i, R_i]\) contains at least one point?
This is a classic “interval stabbing” (or interval covering) problem.
For this problem, a greedy approach is optimal: we process the intervals in ascending order of their right ends, and if a certain interval is not yet stabbed (covered), we place a point at its right end.
This is because when we need to stab an interval \([L, R]\), placing the point at \(R\) is the rightmost possible choice, making it most likely to cover future intervals.
Furthermore, as \(x\) increases, each interval becomes wider, making the condition easier to satisfy.
Thus, the following monotonicity holds:
- If it is possible for \(x\), it is also possible for any value larger than \(x\).
- If it is impossible for \(x\), it is also impossible for any value smaller than \(x\).
Therefore, we can binary search for the answer.
Naively trying all possible streetlight placements is too slow because choosing \(K\) positions out of \(N\) results in a combination count that is far too large to run within the time limit.
Instead, we fix the answer, convert it to a decision problem, and solve it quickly using a greedy approach.
Algorithm
We binary search for the answer \(x\).
In the decision function feasible(x), we perform the following steps:
1. Find the required streetlight range for each section
For each section \(i\), if \(A_i > 0\), we let:
\(r = \left\lfloor \frac{x}{A_i} \right\rfloor\)
To make the danger level of section \(i\) at most \(x\), a streetlight must be placed somewhere in:
\([i-r, i+r]\)
Adjusting to the boundaries of the road, we set:
\(L = \max(1, i-r)\)
\(R = \min(N, i+r)\)
In other words, section \(i\) requires at least one streetlight within the interval \([L, R]\).
Note that for sections where \(A_i = 0\), the danger level is always \(0\) regardless of how far the streetlight is, so we do not need to add any constraints for them.
2. Process intervals in order of their right ends
We want to process all intervals in ascending order of their right ends \(R\).
In the code, instead of storing all intervals in an array and sorting them, we do:
maxL[R] = max(maxL[R], L);
This records only the maximum left end among all intervals with the right end \(R\).
If there are multiple intervals with the same right end, satisfying the one with the largest left end will automatically satisfy all other intervals with smaller left ends.
For example, if we have intervals with right end \(5\) such as:
- \([1, 5]\)
- \([3, 5]\)
- \([4, 5]\)
then \([4, 5]\) is the most restrictive one.
Let last be the position of the last placed streetlight.
We iterate the right end \(r\) from \(1\) to \(N\). If:
if (maxL[r] > 0 && last < maxL[r])
it means the currently placed streetlights cannot cover this interval.
Therefore, we place a new streetlight at position \(r\):
used++;
last = r;
This is the greedy strategy of placing a streetlight at the right end of the current interval.
If the final number of required streetlights used is at most \(K\), the target \(x\) is achievable because any remaining streetlights can be placed arbitrarily in the remaining spots.
3. Binary Search
The search range for the answer is:
- Lower bound: \(-1\)
- Upper bound: \(\max A_i \times (N-1)\)
\(\max A_i \times (N-1)\) is a sufficiently large upper bound because any section can be at most a distance of \(N-1\) away from a streetlight.
In the binary search, if \(x\) is achievable, we decrease the upper bound; otherwise, we increase the lower bound.
Finally, high will be the answer.
Complexity
In the decision function feasible(x), we inspect each section once and then scan the right ends from \(1\) to \(N\), which takes \(O(N)\) time.
The binary search range is at most around \(10^9 \times 10^5 = 10^{14}\), so the number of iterations is \(O(\log(10^{14}))\).
- Time Complexity: \(O(N \log(\max A_i \cdot N))\)
- Space Complexity: \(O(N)\)
Implementation Points
Since \(A_i\) and the candidate answers can be up to around \(10^{14}\), use
long long(or a 64-bit integer type).Sections with \(A_i = 0\) always have a danger level of \(0\), so they can be ignored during the check.
Although we need to place exactly \(K\) streetlights, it is sufficient to check if “the number of required streetlights is at most \(K\)”. This is because any leftover streetlights can be placed in any remaining empty sections.
Storing and sorting all intervals would take \(O(N \log N)\) time, but since the right ends \(R\) are integers from \(1\) to \(N\), using
maxL[R]allows us to perform the check in \(O(N)\) time.Source Code
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, K;
cin >> N >> K;
vector<long long> A(N + 1);
long long maxA = 0;
for (int i = 1; i <= N; i++) {
cin >> A[i];
maxA = max(maxA, A[i]);
}
vector<int> maxL(N + 2);
auto feasible = [&](long long x) -> bool {
fill(maxL.begin(), maxL.end(), 0);
for (int i = 1; i <= N; i++) {
if (A[i] == 0) continue;
long long r = x / A[i];
int L = (r >= i - 1) ? 1 : i - (int)r;
int R = (r >= N - i) ? N : i + (int)r;
maxL[R] = max(maxL[R], L);
}
int used = 0;
int last = 0;
for (int r = 1; r <= N; r++) {
if (maxL[r] > 0 && last < maxL[r]) {
used++;
last = r;
if (used > K) return false;
}
}
return true;
};
long long low = -1;
long long high = maxA * (long long)(N - 1);
while (high - low > 1) {
long long mid = (low + high) / 2;
if (feasible(mid)) high = mid;
else low = mid;
}
cout << high << '\n';
return 0;
}
This editorial was generated by gpt-5.5-high.
posted:
last update: