Official

E - 街灯の配置 / Placement of Street Lights Editorial by admin

GLM 5.2 (High)

Overview

This problem asks us to place \(K\) streetlights in \(N\) sections to minimize the maximum final danger level (\(A_i \times d_i\)) among all sections.

Analysis

Since this is a minimization problem where we want to find the minimum possible maximum value that satisfies a condition, it is natural to use binary search. The feasibility condition for the binary search is: “Can we make the maximum final danger level of all sections at most \(X\)?”

To make the final danger level of a certain section \(i\) at most \(X\), we must have \(A_i \times d_i \le X\), which means the distance \(d_i \le \lfloor X / A_i \rfloor\) (when \(A_i > 0\)). This can be rephrased as: “At least one streetlight must be placed in the interval \([i - R, i + R]\), which is within distance \(R = \lfloor X / A_i \rfloor\) from section \(i\).” We can determine if \(X\) is achievable by finding the minimum number of streetlights required to satisfy this condition for all sections, and checking if this number is at most \(K\).

When given multiple intervals \([L_i, R_i]\) where streetlights must be placed, the problem of finding the minimum number of points (streetlights) to cover all intervals can be solved using a greedy algorithm similar to interval scheduling. Specifically, the method is to sort the intervals in ascending order of their right endpoints, and if there is an interval that is not yet covered, place a streetlight at its right endpoint.

Algorithm

  1. Range of Binary Search: Since the maximum possible danger level is the maximum value of \(A_i\) multiplied by the maximum distance \(N\), we perform a binary search in the range from \(0\) to \(\max(A) \times N\).
  2. Feasibility Check Function:
    • For each section \(i\), we find the right endpoint \(R_{pos} = \min(N-1, i + \lfloor X / A_i \rfloor)\) and the left endpoint \(L = \max(0, i - \lfloor X / A_i \rfloor)\) of the interval where a streetlight must be placed. If \(A_i = 0\), there is no restriction, so we can set \(R = N\) (or a sufficiently large value).
    • To perform the greedy algorithm efficiently, we construct an array max_L_at_R, where max_L_at_R[x] stores the most restrictive (largest) left endpoint among all intervals whose right endpoint is \(x\).
    • We iterate through the array from left to right. If the position of the last placed streetlight last_light is less than max_L_at_R[x], we must place a new streetlight at position \(x\). We count the number of streetlights, and if it exceeds \(K\), we determine it is impossible (False).
  3. Binary Search Update: If the check is successful, we decrease the upper bound; otherwise, we increase the lower bound to pinpoint the minimum value.

Complexity

  • Time Complexity: \(O(N \log(\max(A) \times N))\)
  • Space Complexity: \(O(N)\)

Implementation Points

  • For sections where \(A_i = 0\), the danger level is always \(0\) regardless of the distance, so there is no need to place a streetlight for them. During the search, we can set \(R\) to a sufficiently large value (such as \(N\)) so that they do not impose any constraints.

  • In the greedy algorithm implementation, storing all intervals in an array and sorting them would take \(O(N \log N)\) time. However, by leveraging the fact that the right endpoints are integers from \(0\) to \(N-1\) and using the max_L_at_R array, we can achieve the greedy algorithm in \(O(N)\) time without sorting.

    Source Code

import sys

def solve():
    input = sys.stdin.readline
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    
    max_A = max(A)
    if max_A == 0:
        print(0)
        return
    
    upper = max_A * N
    lower = 0
    ans = upper
    
    while lower <= upper:
        mid = (lower + upper) // 2
        lights = 0
        last_light = -1
        possible = True
        
        max_L_at_R = [-1] * N
        for i in range(N):
            ai = A[i]
            if ai == 0:
                R = N
            else:
                R = mid // ai
            
            L = i - R
            if L < 0:
                L = 0
            
            R_pos = i + R
            if R_pos >= N:
                R_pos = N - 1
            
            if L > max_L_at_R[R_pos]:
                max_L_at_R[R_pos] = L
        
        for x in range(N):
            L = max_L_at_R[x]
            if L != -1:
                if last_light < L:
                    lights += 1
                    last_light = x
                    if lights > K:
                        possible = False
                        break
        
        if possible:
            ans = mid
            upper = mid - 1
        else:
            lower = mid + 1
            
    print(ans)

if __name__ == '__main__':
    solve()

This editorial was generated by or-glm5.2-high.