B - 最大危険度区間 / Maximum Danger Interval Editorial by admin
Qwen3-Coder-480BOverview
Among measurement points on a line, we want to find a contiguous interval that maximizes the “danger level,” which is defined as the number of “caution points” (where the noise level falls within a certain range) minus the number of points that are not caution points.
Analysis
In this problem, we need to calculate “number of caution points - number of non-caution points” for all contiguous intervals and find the maximum value.
If we naively compute this value for all intervals, the number of intervals is \(O(N^2)\), and since \(N\) can be up to \(2 \times 10^5\), this is not practical (TLE).
Therefore, we consider the following transformation: - For each measurement point \(i\), assign a “score” of \(+1\) if it is a caution point, and \(-1\) otherwise. - With this setup, the danger level of any interval \([l, r]\) equals the interval sum of scores:
$\( \sum_{i=l}^{r} \text{score}_i \)$
To compute this interval sum efficiently, we use prefix sums: - \( \text{prefix\_sum}[i] = \sum_{j=0}^{i-1} \text{score}_j \) - The sum of interval \([l, r]\) is \( \text{prefix\_sum}[r+1] - \text{prefix\_sum}[l] \)
Therefore, the maximum danger level is:
\[ \max_{0 \leq l < r \leq N} (\text{prefix\_sum}[r] - \text{prefix\_sum}[l]) \]
In other words, we need to find the maximum difference between “the prefix sum up to some point” and “the minimum prefix sum before that point.”
Algorithm
- For each measurement point \(i\), set the score to \(+1\) if \(L \leq T_i \leq R\), and \(-1\) otherwise.
- Precompute the prefix sum of scores (an array of length \(N+1\)).
- Scan the prefix sums from left to right, and:
- Compute the value of the current prefix sum minus the minimum so far, and update the maximum.
- If the current prefix sum is a new minimum, update the minimum.
This allows us to obtain the solution in \(O(N)\).
Complexity
- Time complexity: \(O(N)\)
- Space complexity: \(O(N)\)
Implementation Notes
By initializing the prefix sum array with \(0\) at the beginning, we can easily compute the sum of interval \([0, i)\).
The logic of maintaining the minimum while updating the maximum difference is a classic “prefix sum + minimum tracking” pattern.
Source Code
import sys
from itertools import accumulate
def main():
import sys
input = sys.stdin.read
data = input().split()
N = int(data[0])
L = int(data[1])
R = int(data[2])
T = list(map(int, data[3:3+N]))
# 各地点が注意地点かどうかを判定し、+1 (注意地点) または -1 (非注意地点) を割り当てる
# 危険度 = 区間内の (注意地点数) - (非注意地点数)
# よって、+1 が注意地点、-1 が非注意地点に対応するスコアと考えると、
# 区間和 = (注意地点数) - (非注意地点数) となる
scores = []
for t in T:
if L <= t <= R:
scores.append(1)
else:
scores.append(-1)
# 累積和を使って、任意区間の和を高速に計算できるようにする
# prefix_sums[0] = 0
# prefix_sums[i] = scores[0] + ... + scores[i-1]
prefix_sums = [0] + list(accumulate(scores))
# 危険度 = prefix_sums[right] - prefix_sums[left] (1 <= left < right <= N)
# 最大の危険度 = max(prefix_sums[right] - min(prefix_sums[0..right-1]))
max_danger = float('-inf')
min_prefix = 0 # prefix_sums[0] = 0
for i in range(1, N+1):
current_sum = prefix_sums[i]
danger = current_sum - min_prefix
if danger > max_danger:
max_danger = danger
if current_sum < min_prefix:
min_prefix = current_sum
print(max_danger)
if __name__ == "__main__":
main()
This editorial was generated by qwen3-coder-480b.