公式

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

claude4.8opus-high

Summary

This problem asks us to place \(K\) streetlights in \(N\) sections to minimize the maximum final risk (distance from the streetlight \(\times\) original risk) among all sections. This typical structure of “minimizing the maximum value” allows us to solve the problem using binary search.

Analysis

First, if we try to directly minimize the maximum risk, there are \(\binom{N}{K}\) possible streetlight placements, which is impossible to try all of them.

An important observation here is that “minimizing the maximum value” works very well with binary search. That is, we can consider the decision problem: “Is it possible to make the maximum final risk at most \(X\)?”

This decision problem has the following monotonicity:

  • If it is achievable (feasible) for a certain value \(X\), it is always achievable for any value greater than \(X\).
  • If it is unachievable for a certain value \(X\), it is always unachievable for any value smaller than \(X\).

Thus, we only need to find the minimum \(X\) that is achievable, which can be done using binary search on \(X\).

Reformulating the Decision Problem

Let’s consider “Is it possible to make the maximum risk at most \(X\)?”. For each section \(j\), let \(d_j\) be the distance to the nearest streetlight. The risk of section \(j\) is \(A_j \times d_j\). For this to be at most \(X\):

\[A_j \times d_j \le X \iff d_j \le \frac{X}{A_j}\]

  • When \(A_j = 0\): The risk is always \(0\), so there is no constraint even if there is no streetlight nearby.
  • When \(A_j > 0\): We must satisfy \(d_j \le \lfloor X / A_j \rfloor\). If we let \(r_j = \lfloor X / A_j \rfloor\), this means a streetlight must be placed somewhere within a distance of \(r_j\) from section \(j\), i.e., in the interval \([\,j - r_j,\; j + r_j\,]\) (clamped to the range \([1, N]\)).

Therefore, the decision problem is reduced to:

“Can we cover all the intervals \([L_j, R_j]\) given for each section \(j\) using at most \(K\) points (streetlight positions)?”

which is the interval covering problem (stabbing intervals with points).

Algorithm

Greedy Algorithm for Interval Covering

The problem of “stabbing all intervals with the minimum number of points” can be solved using a greedy approach:

  1. Sort all intervals in ascending order of their right endpoint \(R\).
  2. Whenever an uncovered interval appears, place a point (streetlight) at its right endpoint \(R\).
  3. Placing the point at the right endpoint maximizes the coverage for subsequent intervals.

Specifically, we maintain the position of the last placed point as last. Only when the left endpoint left of an interval is strictly greater than last (meaning the interval is not yet covered), we place a new point at its right endpoint right. It is well known that this greedy strategy yields the minimum number of points.

If this minimum number of points is at most \(K\), then feasible(X) = true; otherwise, false.

Overall Flow

  • The range of the binary search is \([0,\; A_{\max} \times (N-1)]\) (the range of possible maximum risk values).
  • For each \(X\), determine feasible(X) and find the minimum achievable \(X\).

Example

For example, suppose that for a fixed \(X\), the required coverage intervals for each section are \([1,3], [2,4], [5,6]\). Sorting them by their right endpoints and processing them greedily:

  • \([1,3]\): Uncovered, so place a streetlight at the right endpoint \(3\) \(\to\) last=3, count = 1.
  • \([2,4]\): Covered since the left endpoint \(2 \le 3\).
  • \([5,6]\): Uncovered since the left endpoint \(5 > 3\), so place a streetlight at the right endpoint \(6\) \(\to\) last=6, count = 2.

Since we only need \(2\) streetlights in total, it is feasible if \(K \ge 2\).

Complexity

Since the binary search range is at most \(A_{\max} \times (N-1) \le 10^9 \times 10^5 = 10^{14}\), the number of iterations is \(O(\log(A_{\max} N))\) (about \(50\) times). In each decision step, sorting the intervals takes \(O(N \log N)\) time.

  • Time Complexity: \(O(N \log N \cdot \log(A_{\max} N))\)
  • Space Complexity: \(O(N)\)

Implementation Details

  • Calculating the maximum distance: \(r_j = \lfloor X / A_j \rfloor\) can be calculated using integer division (floored). This corresponds to the maximum \(d_j\) satisfying \(A_j \times d_j \le X\).

  • Preventing overflow: Since \(A_{\max} \times (N-1)\) can be as large as \(10^{14}\), we must use a 64-bit integer type such as long long.

  • Handling \(A_j = 0\): The risk is always \(0\), so it does not impose any constraints. We should avoid adding these as intervals.

  • Clamping the intervals: Since the section indices are from \(1\) to \(N\), we should clamp left so it is not less than \(1\), and right so it does not exceed \(N\).

  • Sort key: In the greedy algorithm, we sort the intervals by their right endpoints. Thus, storing them as pair<right, left> is convenient as they can be sorted directly in ascending order of their right endpoints.

    Source Code

#include <bits/stdc++.h>
using namespace std;

int N;
long long K;
vector<long long> A;

bool feasible(long long X) {
    // intervals (right, left)
    static vector<pair<long long,long long>> iv;
    iv.clear();
    for (int j = 1; j <= N; j++) {
        long long a = A[j];
        if (a == 0) continue; // no constraint
        long long r = X / a; // floor
        long long left = (long long)j - r;
        if (left < 1) left = 1;
        long long right = (long long)j + r;
        if (right > N) right = N;
        iv.push_back({right, left});
    }
    sort(iv.begin(), iv.end());
    long long count = 0;
    long long last = 0; // no point chosen yet
    for (auto &p : iv) {
        long long right = p.first;
        long long left = p.second;
        if (left > last) {
            // need new point
            last = right;
            count++;
            if (count > K) return false;
        }
    }
    return count <= K;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> N >> K;
    A.assign(N+1, 0);
    long long maxA = 0;
    for (int i = 1; i <= N; i++) {
        cin >> A[i];
        maxA = max(maxA, A[i]);
    }
    
    long long lo = 0, hi = maxA * (long long)(N - 1);
    if (hi < 0) hi = 0;
    // find smallest X with feasible(X) true
    // feasible is monotonic: larger X -> easier
    while (lo < hi) {
        long long mid = lo + (hi - lo) / 2;
        if (feasible(mid)) hi = mid;
        else lo = mid + 1;
    }
    cout << lo << "\n";
    return 0;
}

This editorial was generated by claude4.8opus-high.