E - 街灯の配置 / Placement of Street Lights Editorial by admin Editorial for printing
claude4.8opus-highSummary
This problem asks us to place \(K\) streetlights to minimize the “maximum final risk among all sections.” We can binary search for the answer and determine whether a candidate value is achievable using a greedy approach to find the minimum number of points needed to intersect a set of intervals.
Analysis
Direct Optimization is Difficult
There are \(\binom{N}{K}\) possible ways to place the streetlights, making exhaustive search impossible. Additionally, problems of the form “minimize the maximum value” are generally difficult to handle if we try to optimize the placement directly.
“Minimizing the Maximum” \(\to\) Binary Search
For this type of problem, it is a standard technique to binary search on the answer itself.
That is, we consider the decision problem: “Is it possible to make the maximum final risk at most \(X\)?” - The larger \(X\) is, the looser (easier to satisfy) the condition becomes. - The smaller \(X\) is, the stricter the condition becomes.
Since this monotonicity holds, we can find the minimum achievable \(X\) using binary search.
Reformulating the Decision Problem as Interval Covering
For a given \(X\), we want to determine if we can satisfy \(A_j \times d_j \le X\) for all sections \(j\).
For each section \(j\) (where \(A_j > 0\)), the distance \(d_j\) to the nearest streetlight must satisfy: $\(A_j \times d_j \le X \iff d_j \le \left\lfloor \frac{X}{A_j} \right\rfloor\)\( If we let \)r = \lfloor X / A_j \rfloor\(, this means there must be **at least one streetlight** within distance \)r\( from section \)j\(, which corresponds to the interval: \)\([\,\max(0,\,j-r),\ \min(N-1,\,j+r)\,]\)\( (Sections with \)A_j = 0\( have a risk of \)0$ regardless of the distance, so they impose no constraints).
Thus, the decision problem can be reformulated as follows:
Given the intervals required by each section \(j\), we want to place points (streetlights) such that every interval contains at least one point. Is the minimum number of points required at most \(K\)?
Since placing extra streetlights does no harm and \(K \le N\), if the minimum number of points is at most \(K\), it is achievable.
Algorithm
Minimum Number of Points to Intersect Intervals (Greedy Approach)
The “minimum number of points to cover all intervals” can be found using a greedy approach: sort the intervals by their right endpoints, and whenever an uncovered interval is encountered, place a point at its right endpoint. Placing the point at the right endpoint covers as many subsequent intervals as possible.
In our implementation, we group intervals by their right endpoints to improve efficiency:
- We store the “maximum left endpoint among all intervals with right endpoint \(r\)” in maxleft[r].
- Even if there are multiple intervals with the same right endpoint \(r\), the interval with the largest left endpoint is the hardest to cover. If this interval is covered by an existing point, all others with the same right endpoint are also covered. If it is not covered, placing a point at the right endpoint \(r\) covers all of them. Therefore, it is sufficient to keep track of only maxleft.
We iterate through the right endpoints \(v = 0, 1, \ldots, N-1\) in order. Let last be the position of the last placed point. If maxleft[v] > last (meaning the interval is uncovered), we place a point at \(v\) and increment count. If count > K, we determine that it is impossible.
Range of Binary Search
- Lower bound:
lo = 0 - Upper bound:
hi = maxA * (N-1)(the worst-case scenario where the section with the highest risk coefficient is at one end and the streetlight is at the other end)
If all elements are \(0\), the answer is \(0\), which is handled as a special case.
Complexity
- Time Complexity: \(O(N \log(A_{\max} \cdot N))\) (repeating the \(O(N)\) decision step during binary search)
- Space Complexity: \(O(N)\)
Implementation Details
Sections with \(A_j = 0\) do not impose any constraints, so we skip them (do not add them to the intervals).
r = X // A_jusing integer division represents the maximum integer distance satisfying \(d_j \le X/A_j\).Clamp the left and right endpoints of the intervals to \([0, N-1]\).
In the greedy check, by grouping by the right endpoint and keeping only the maximum left endpoint, we achieve an \(O(N)\) check without sorting. We initialize
maxleft[r]to-1to distinguish them from right endpoints where no intervals end.Source Code
import sys
def main():
data = sys.stdin.buffer.read().split()
N = int(data[0]); K = int(data[1])
A = [int(x) for x in data[2:2+N]]
maxA = max(A)
if maxA == 0:
print(0)
return
hi = maxA * (N - 1) if N > 1 else 0
lo = 0
def feasible(X):
maxleft = [-1] * N
Aloc = A
for j in range(N):
a = Aloc[j]
if a == 0:
continue
r = X // a
left = j - r
if left < 0:
left = 0
right = j + r
if right >= N:
right = N - 1
if left > maxleft[right]:
maxleft[right] = left
last = -(1 << 60)
count = 0
for v in range(N):
ml = maxleft[v]
if ml >= 0 and ml > last:
count += 1
last = v
if count > K:
return False
return True
while lo < hi:
mid = (lo + hi) // 2
if feasible(mid):
hi = mid
else:
lo = mid + 1
print(lo)
main()
This editorial was generated by claude4.8opus-high.
posted:
last update: