B - 最大危険度区間 / Maximum Danger Interval Editorial by admin Editorial for printing
gemini-3-flash-thinkingOverview
Given \(N\) measurement points with noise levels, convert each to \(+1\) or \(-1\) depending on whether it satisfies a condition, then find the “maximum sum of a contiguous subarray” in the resulting numerical sequence.
Analysis
First, let’s organize the definition of “danger level” from the problem statement. - Alert point (\(L \leq T_i \leq R\)): \(+1\) - Not an alert point: \(-1\)
After this conversion, the problem can be reformulated into the very simple problem of “finding the contiguous interval with the maximum sum in a sequence consisting of \(+1\) and \(-1\)”.
Naive Approach
If we examine all combinations of intervals \((i, j)\), the number of intervals is \(O(N^2)\). Since \(N = 2 \times 10^5\), the number of computations would be approximately \(4 \times 10^{10}\) at most, which will not finish within the time limit.
Efficient Approach
The problem of finding the “maximum sum of a contiguous subarray” can be solved in \(O(N)\) time complexity using Kadane’s algorithm. It exploits the property that when computing a running cumulative sum up to a certain point, if that sum becomes negative, it is better to discard the interval so far and start a new interval from the next point, as this will lead to a larger total sum going forward.
Algorithm
Kadane’s Algorithm
- Initialize a variable
max_dangerto hold the maximum danger level with a very small value (or the value of the first element). - Initialize a variable
current_sumto hold the sum of the current interval with \(0\). - For each point \(T_i\), repeat the following:
- If \(T_i\) is an alert point, set \(val = 1\); otherwise, set \(val = -1\).
- Add \(val\) to
current_sum. - Update
max_dangerwithmax(max_danger, current_sum). - If
current_sumbecomes negative, resetcurrent_sum = 0(because rather than proceeding with a negative sum, it is more advantageous to start a new interval from the next point).
Complexity
- Time Complexity: \(O(N)\)
- Since we only scan the sequence once, the computation finishes in linear time with respect to the number of measurement points \(N\).
- Space Complexity: \(O(N)\)
- We use memory for \(N\) elements to store the input values.
Implementation Notes
Careful with initial values: Since the maximum danger level can be negative (e.g., when none of the points are alert points),
max_dangermust be initialized to a sufficiently small value or initialized with the evaluation value of the first point.Fast I/O: When processing input of size \(N = 2 \times 10^5\) in Python, reading all input at once using
sys.stdin.read().split()is faster than callinginput()repeatedly, reducing execution time.Source Code
import sys
def solve():
# 入力を一度に読み込み、空白で分割してリスト化します(高速な入出力のため)
input_data = sys.stdin.read().split()
if not input_data:
return
# 計測地点の数 N、下限値 L、上限値 R を取得
N = int(input_data[0])
L = int(input_data[1])
R = int(input_data[2])
# 危険度の最大値を保持する変数(非常に小さい値で初期化)
# すべての区間(長さ1以上)の中での最大値を求めるため、
# 最小の可能性である -N よりも小さい値、または -float('inf') を使います。
max_danger = -float('inf')
# 現在の区間の累積危険度を保持する変数
current_sum = 0
# 各計測地点の騒音レベル T_i は input_data のインデックス 3 以降に格納されています
# カダネのアルゴリズム(Kadane's algorithm)を用いて、連続する部分配列の最大和を求めます
for i in range(3, 3 + N):
t = int(input_data[i])
# 注意地点であれば +1、そうでなければ -1 を危険度に加算
if L <= t <= R:
val = 1
else:
val = -1
current_sum += val
# これまでの最大危険度を更新
if current_sum > max_danger:
max_danger = current_sum
# もし累積危険度が 0 を下回った場合、それまでの区間をリセットして
# 次の地点から新しい区間を開始する方が有利になるため、0 にリセットします
if current_sum < 0:
current_sum = 0
# 最大危険度を出力
print(max_danger)
if __name__ == '__main__':
solve()
This editorial was generated by gemini-3-flash-thinking.
posted:
last update: