A - 連続上昇気温 / Consecutive Rising Temperatures 解説 by admin
Gemini 3.0 FlashOverview
Given \(N\) days of temperature data, the problem asks to find the maximum number of consecutive days during which the temperature is strictly (greater than) increasing.
Analysis
The key insight of this problem is: “When the increasing streak breaks, record the length so far and start counting anew.”
Naive Approach
If we examine all combinations of “start day” and “end day” (\(O(N^2)\) pairs), since \(N\) can be up to \(2 \times 10^5\), the number of computations would be approximately \(4 \times 10^{10}\), which will not finish within the time limit.
Efficient Approach
Consider scanning the temperature array from left to right just once. - If the temperature on day \(i\) is higher than on day \(i-1\): The increasing period continues, so increment the current count by 1. - If the temperature on day \(i\) is less than or equal to day \(i-1\): The increasing period has been broken. Record the maximum of the count so far and reset the count to 1 (the length of that day alone).
In this way, we can find the answer with a single pass.
Algorithm
The problem can be solved with the following steps:
- Initialize a variable
max_lento hold the maximum length and a variablecurrent_lento hold the length of the current increasing period, both set to \(1\). - From day \(2\) to day \(N\), perform the following in order:
- If “today’s temperature > yesterday’s temperature”, increment
current_lenby \(1\). - Otherwise, update
max_lenwithmax(max_len, current_len)and resetcurrent_lento \(1\).
- If “today’s temperature > yesterday’s temperature”, increment
- After checking all days, update
max_lenone more time withmax(max_len, current_len)to account for the possibility that the last increasing period is the longest. - Output
max_len.
Complexity
- Time Complexity: \(O(N)\)
- Since we scan the array only once, it finishes in time proportional to the number of elements \(N\).
- Space Complexity: \(O(N)\)
- \(O(N)\) memory is used to store the input temperature data in a list.
Implementation Notes
“Strictly greater” check: Since the problem states \(A_l < A_{l+1} < \cdots\), note that when temperatures are equal (\(A_i = A_{i-1}\)), the increasing period is broken.
Update after loop ends: If the temperature keeps increasing until the last day, the
elsebranch (update during reset) inside the loop is never executed. Therefore,max_lenmust be updated one final time outside the loop.Fast input: Since \(N\) can be large, in Python it is faster to read all input at once using
sys.stdin.read().split()or similar methods.Source Code
import sys
def solve():
# 標準入力からすべてのデータを取得
input_data = sys.stdin.read().split()
if not input_data:
return
# Nの取得
n = int(input_data[0])
# 気温データの取得
if n == 0:
print(0)
return
a = list(map(int, input_data[1:]))
max_len = 1
current_len = 1
# 配列を走査して連続上昇期間を計算
for i in range(1, n):
if a[i] > a[i-1]:
current_len += 1
else:
if current_len > max_len:
max_len = current_len
current_len = 1
# 最後の要素まで上昇が続いていた場合の更新
if current_len > max_len:
max_len = current_len
print(max_len)
if __name__ == "__main__":
solve()
This editorial was generated by gemini-3-flash-preview.
投稿日時:
最終更新: