B - 最大危険度区間 / Maximum Danger Interval Editorial by admin
Claude 4.6 Opus (Thinking)Overview
The problem asks you to choose a single contiguous interval and find the maximum value of “number of caution points − number of non-caution points” within it. By converting each point to \(+1\) or \(-1\), this can be solved using the maximum subarray sum (Kadane’s algorithm).
Analysis
Reformulating the Problem
For each measurement point \(i\), if the noise level \(T_i\) satisfies \(L \leq T_i \leq R\), it is a “caution point.”
The danger level of an interval \([l, r]\) is defined as:
\[\text{Danger level} = (\text{number of caution points in the interval}) - (\text{number of non-caution points in the interval})\]
Here, we assign a value to each point:
\[a_i = \begin{cases} +1 & (L \leq T_i \leq R \text{: caution point}) \\ -1 & (\text{otherwise: non-caution point}) \end{cases}\]
Then, the danger level of an interval \([l, r]\) can be expressed as \(\sum_{i=l}^{r} a_i\). Therefore, the problem reduces to finding the maximum contiguous subarray sum of the sequence \(a_1, a_2, \ldots, a_N\).
Issues with the Naive Approach
Enumerating all intervals \([l, r]\) (\(1 \leq l \leq r \leq N\)) gives \(O(N^2)\) intervals, which results in TLE since \(N\) can be up to \(2 \times 10^5\).
Solution
Using Kadane’s algorithm, we can find the maximum subarray sum in \(O(N)\).
Algorithm
Kadane’s algorithm scans the array from left to right while maintaining “the maximum sum of a subarray ending at the current position.”
Specifically, we prepare variables max_ending_here and max_so_far, and for each element \(a_i\):
max_ending_here = max(a_i, max_ending_here + a_i)- Choose the better option between starting a new subarray from \(a_i\) or appending \(a_i\) to the existing subarray
max_so_far = max(max_so_far, max_ending_here)- Update the overall maximum
Concrete example: For \(N=5, L=3, R=7, T = [2, 5, 4, 1, 6]\)
| \(i\) | \(T_i\) | \(a_i\) | max_ending_here |
max_so_far |
|---|---|---|---|---|
| 1 | 2 | \(-1\) | \(-1\) | \(-1\) |
| 2 | 5 | \(+1\) | \(\max(1, -1+1)=1\) | \(1\) |
| 3 | 4 | \(+1\) | \(\max(1, 1+1)=2\) | \(2\) |
| 4 | 1 | \(-1\) | \(\max(-1, 2-1)=1\) | \(2\) |
| 5 | 6 | \(+1\) | \(\max(1, 1+1)=2\) | \(2\) |
The answer is \(2\) (for example, the interval \([2,3]\) gives \(a_2 + a_3 = 1 + 1 = 2\)).
Complexity
- Time complexity: \(O(N)\) — only a single scan through the array
- Space complexity: \(O(1)\) — aside from reading the input, only a few variables are used
Implementation Notes
Since the interval length must be at least \(1\), we initialize
max_so_farto \(-\infty\) to ensure that at least one element is always selected.If none of the points are caution points, the answer is \(-1\) (selecting just one point). This is handled correctly as well.
By reading the input all at once with
sys.stdin.read(), the solution can run efficiently even in Python.Source Code
import sys
def solve():
input_data = sys.stdin.read().split()
N = int(input_data[0])
L = int(input_data[1])
R = int(input_data[2])
# Each point: if L <= T_i <= R, it's a "注意地点" contributing +1, else -1
# We want the maximum subarray sum (Kadane's algorithm), with subarray length >= 1
max_ending_here = 0
max_so_far = -float('inf')
for i in range(N):
t = int(input_data[3 + i])
if L <= t <= R:
val = 1
else:
val = -1
max_ending_here = max(val, max_ending_here + val)
max_so_far = max(max_so_far, max_ending_here)
print(max_so_far)
solve()
This editorial was generated by claude4.6opus-thinking.