E - 街灯の配置 / Placement of Street Lights 解説 by admin
gpt-5.5-highSummary
The problem asks us to choose exactly \(K\) plots out of \(N\) to place streetlights in order to minimize the maximum value of “danger value \(\times\) distance to the nearest streetlight” across all plots.
We can binary search the answer, quickly checking: “Is it possible to make the maximum danger value at most \(x\)?”
Analysis
Let’s consider whether we can make the final maximum danger value at most \(x\).
For a plot \(i\) with an initial danger value of \(A_i\), let \(d_i\) be its distance to the nearest streetlight. The condition is:
\(A_i \times d_i \leq x\)
If \(A_i > 0\), this can be rewritten as:
\(d_i \leq \left\lfloor \frac{x}{A_i} \right\rfloor\)
In other words, to make plot \(i\) safe, we must place at least one streetlight within the range from
\(i - \left\lfloor \frac{x}{A_i} \right\rfloor\) to
\(i + \left\lfloor \frac{x}{A_i} \right\rfloor\)
Clipping this range so that it lies within the road boundaries \([1, N]\) gives us the interval
\([L_i, R_i]\)
Thus, the question “Is it possible to make the maximum danger value at most \(x\)?” reduces to the following problem:
We have an interval \([L_i, R_i]\) corresponding to each plot \(i\).
Are at most \(K\) points sufficient to stab (cover) all intervals?
This is a classic “interval stabbing problem.”
Note that plots with \(A_i = 0\) can be ignored because their final danger value is always \(0\) regardless of the distance.
Additionally, if a condition \(x\) is achievable, any larger \(x\) is also achievable.
Therefore, we can binary search the answer.
A naive approach of trying all possible streetlight placements would result in \(\binom{N}{K}\) combinations, which is far too slow for \(N \leq 10^5\).
Instead, we combine binary search with a greedy algorithm on intervals to perform the check quickly.
Algorithm
First, we extract only the plots where \(A_i > 0\).
If the number of plots with \(A_i > 0\) is \(K\) or less, we can place streetlights on all of them to make the maximum danger value \(0\).
In this case, the answer is \(0\).
Otherwise, we binary search the answer.
Decision Function ok(x)
This function determines whether “the maximum danger value can be at most \(x\).”
For each plot with \(A_i > 0\), let
\(r = \left\lfloor \frac{x}{A_i} \right\rfloor\)
To satisfy the condition for this plot, a streetlight must be placed in the range
\([i-r, i+r]\)
Since the road is bounded between \(1\) and \(N\), we actually set
\(L = \max(1, i-r)\)
\(R = \min(N, i+r)\)
This gives us the interval \([L, R]\).
Next, we find the minimum number of points needed to stab all intervals.
If we process the intervals in ascending order of their right endpoints \(R\), when we encounter an interval that is not yet stabbed, it is optimal to place a point at its right endpoint \(R\).
This is because: - Consider the unprocessed interval with the smallest right endpoint. - To stab this interval, we must place a point within it. - Placing the point as far to the right as possible maximizes the chance of stabbing subsequent intervals. - Therefore, placing it at the right endpoint of this interval is optimal.
In the implementation, instead of actually sorting the intervals, we can group the information by their right endpoints.
Let maxL[R] be “the maximum left endpoint among all intervals whose right endpoint is \(R\).”
Then we iterate \(R = 1, 2, \ldots, N\):
last: the position of the last placed streetlightcnt: the number of streetlights placed so far
For intervals with right endpoint \(R\), if maxL[R] <= last, then these intervals are already stabbed by the last placed streetlight.
On the other hand, if maxL[R] > last, there exists an interval whose left endpoint is to the right of last, meaning it cannot be stabbed by the current streetlight.
Thus, we must place a new streetlight at position \(R\).
If cnt exceeds \(K\) during this process, then \(x\) is unachievable.
Binary Search
We set the lower bound of the binary search to low = -1 and the upper bound to
\(\max A_i \times (N-1)\)
Since the maximum possible distance is \(N-1\) in the worst case, we can definitely achieve a danger value of at most this upper bound.
We use binary search to find the minimum achievable \(x\).
Complexity
- Time Complexity: \(O(N \log(\max A_i \cdot N))\)
- Space Complexity: \(O(N)\)
The decision function ok(x) runs in \(O(N)\) time because it inspects the plots with positive \(A_i\) once and then iterates from \(1\) to \(N\).
The binary search is performed approximately \(O(\log(\max A_i \cdot N))\) times.
Implementation Details
Plots with \(A_i = 0\) always have a danger value of \(0\), so we exclude them from the decision process.
This also prevents division-by-zero errors in \(x // A_i\).
Furthermore, the decision function checks if we can stab all intervals with “at most \(K\)” points.
Although the problem requires placing “exactly \(K\)” streetlights, if we can satisfy the conditions with fewer than \(K\) streetlights, we can simply place the remaining streetlights on any empty plots, which does not affect the correctness.
Source Code
import sys
def main():
data = list(map(int, sys.stdin.buffer.read().split()))
N, K = data[0], data[1]
A = data[2:]
pos = []
max_a = 0
for i, a in enumerate(A, 1):
if a:
pos.append((i, a))
if a > max_a:
max_a = a
if len(pos) <= K:
print(0)
return
n = N
k = K
def ok(x):
maxL = [0] * (n + 1)
for i, a in pos:
r = x // a
if r >= n:
L = 1
R = n
else:
L = i - r
if L < 1:
L = 1
R = i + r
if R > n:
R = n
if L > maxL[R]:
maxL[R] = L
last = 0
cnt = 0
for r in range(1, n + 1):
if maxL[r] > last:
cnt += 1
if cnt > k:
return False
last = r
return True
low = -1
high = max_a * (n - 1)
while high - low > 1:
mid = (low + high) // 2
if ok(mid):
high = mid
else:
low = mid
print(high)
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.5-high.