E - 街灯の配置 / Placement of Street Lights Editorial by admin Editorial for printing
gemini-3.5-flash-highOverview
This problem asks us to minimize the “maximum final danger level among all sections” by optimizing the placement of streetlights. This is a classic “minimize the maximum value” problem, which can be solved efficiently by combining binary search and a greedy algorithm.
Analysis
1. “Minimize the Maximum” via Binary Search
Consider the decision problem: “Is it possible to make the maximum final danger level at most \(X\)?” If it is achievable for some value \(X\), then it is also achievable for any \(X' > X\). Conversely, if it is not achievable, then it is also not achievable for any smaller value. Since monotonicity holds with respect to the answer, we can find the optimal \(X\) using binary search.
2. Reformulating the Decision Problem
To make the maximum danger level at most \(X\), we must ensure that the final danger level for every section \(i\) is at most \(X\). Let \(A_i\) be the original danger level of section \(i\), and \(d_i\) be the distance to the nearest streetlight. The condition to be satisfied is \(A_i \times d_i \le X\), which rewrites to \(d_i \le \lfloor X / A_i \rfloor\).
This means “at least one streetlight must be placed within a distance of \(\lfloor X / A_i \rfloor\) from section \(i\).” For each section \(i\), if we represent the range of indices where a streetlight can be placed as \([L_i, R_i]\), we get: - \(L_i = \max(1, i - \lfloor X / A_i \rfloor)\) - \(R_i = \min(N, i + \lfloor X / A_i \rfloor)\)
Thus, the decision problem can be reformulated as follows:
“Given \(N\) intervals \([L_i, R_i]\), place streetlights such that every interval contains at least one streetlight. Is the minimum number of streetlights required at most \(K\)?”
3. Greedy Algorithm for Interval Covering
This problem can be solved using a standard greedy algorithm. We sort the intervals in ascending order of their right endpoints \(R_i\) and process them from left to right.
Let \([L, R]\) be the uncovered interval with the smallest right endpoint. To cover this interval, it is optimal to place a streetlight at the right endpoint \(R\). This is because placing the streetlight as far right as possible maximizes the chance of covering other intervals (whose right endpoints are at least \(R\)) that will be processed later.
Specifically, we can find the minimum number of required streetlights with the following steps:
1. Sort all intervals in ascending order of their right endpoints \(R_i\).
2. Let the position of the last placed streetlight be last_point (initially \(-1\)).
3. Iterate through the sorted intervals. If the left endpoint \(L_i\) of the current interval is greater than last_point (i.e., it is not yet covered), place a new streetlight at \(R_i\) and update last_point = R_i.
Algorithm
Setting the Binary Search Range We perform a binary search with the minimum possible answer
lowas \(0\) and the maximum possible answerhighas \(2 \times 10^{14}\) (since \(A_i \le 10^9\) and the maximum distance is \(N \le 10^5\), the maximum danger level is around \(10^{14}\)).Fast Sorting (Application of Counting Sort) In the decision problem (the
checkfunction), we need to sort the intervals in ascending order of \(R_i\). Using a standard sort (\(O(N \log N)\)) would make the overall execution time too slow. However, since \(R_i\) is an integer in the range from \(1\) to \(N\), we can sort the intervals in \(O(N)\) using counting sort (bucket sort).- Count the occurrences of each \(R\) value, and use prefix sums to determine the starting index for each \(R\).
- This allows us to construct an array
sorted_Lcontaining the \(L_i\) values sorted by their corresponding \(R_i\) in \(O(N)\) time.
Decision Apply the greedy algorithm to the sorted intervals. If the required number of streetlights is at most \(K\), return
true; otherwise, returnfalse.
Complexity
Time Complexity: \(O(N \log(\max(A) \cdot N))\) The number of binary search steps is \(\log_2(2 \times 10^{14}) \approx 48\). Since the
checkfunction in each step runs in \(O(N)\) time using counting sort and the greedy algorithm, the overall time complexity is \(O(N \log(\max(A) \cdot N))\), which runs very fast and easily passes within the time limit.Space Complexity: \(O(N)\) We use \(O(N)\) memory to store the interval information and arrays for counting sort (such as
L_arr,R_arr,sorted_L,count_r, etc.).
Implementation Details
Avoiding Division by Zero For sections with \(A_i = 0\), the final danger level will always be \(0\) regardless of where the streetlight is placed (i.e., there is no distance constraint). In this case, calculating \(\lfloor X / A_i \rfloor\) would result in a division-by-zero error. Thus, we handle this as an exception by setting the coverable distance to infinity (represented as \(N\) in the code).
Handling Overflow Since the binary search range and danger calculations can exceed \(10^{14}\), it is necessary to use 64-bit integers (
long longin C++) to prevent overflow.Source Code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int N, K;
vector<long long> A;
vector<int> count_r;
vector<int> pos;
vector<int> sorted_L;
vector<int> cur_pos;
vector<int> L_arr;
vector<int> R_arr;
bool check(long long X) {
fill(count_r.begin(), count_r.end(), 0);
for (int i = 1; i <= N; ++i) {
long long r_dist;
if (A[i] == 0) {
r_dist = N;
} else {
r_dist = X / A[i];
}
int L = max(1LL, i - r_dist);
int R = min((long long)N, i + r_dist);
L_arr[i] = L;
R_arr[i] = R;
count_r[R]++;
}
pos[1] = 0;
for (int r = 1; r <= N; ++r) {
pos[r + 1] = pos[r] + count_r[r];
}
for (int r = 1; r <= N; ++r) {
cur_pos[r] = pos[r];
}
for (int i = 1; i <= N; ++i) {
int R = R_arr[i];
sorted_L[cur_pos[R]++] = L_arr[i];
}
int count = 0;
int last_point = -1;
for (int r = 1; r <= N; ++r) {
int start = pos[r];
int end = pos[r + 1];
for (int idx = start; idx < end; ++idx) {
int L = sorted_L[idx];
if (L > last_point) {
last_point = r;
count++;
}
}
}
return count <= K;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
if (!(cin >> N >> K)) return 0;
A.resize(N + 1);
for (int i = 1; i <= N; ++i) {
cin >> A[i];
}
count_r.resize(N + 2);
pos.resize(N + 2);
sorted_L.resize(N);
cur_pos.resize(N + 2);
L_arr.resize(N + 1);
R_arr.resize(N + 1);
long long low = 0;
long long high = 200000000000000LL; // 2 * 10^14
long long ans = high;
while (low <= high) {
long long mid = low + (high - low) / 2;
if (check(mid)) {
ans = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
cout << ans << "\n";
return 0;
}
This editorial was generated by gemini-3.5-flash-high.
posted:
last update: