A - 連続上昇気温 / Consecutive Rising Temperatures Editorial by admin
Qwen3-Coder-480BOverview
Given a sequence of temperatures, find the maximum length of a strictly monotonically increasing contiguous subsequence.
Discussion
In this problem, we need to find the longest among all “consecutively increasing intervals.”
For example, if the temperatures are recorded as [10, 12, 15, 14, 16, 17, 18]:
- The first 3 days [10, 12, 15] are consecutively increasing
- The next day [14] breaks the streak
- After that, 4 days [14, 16, 17, 18] form a new consecutively increasing interval
Therefore, the longest is the latter interval of 4 days.
A naive approach would be to enumerate all intervals and check whether each one is an increasing sequence, but this would be \(O(N^3)\) or \(O(N^2)\), which cannot be processed in time for the constraint \(N \leq 2 \times 10^5\).
Instead, by only checking “whether the value has increased compared to the previous day” and updating the length of the current consecutively increasing interval while scanning, we can obtain the answer in a single loop.
Algorithm
- Initialize the length of the interval including the first day as
current_length = 1. - Compare with the previous day; if it has increased, increment
current_length. - At the same time, update the running maximum
max_length. - If it has not increased, reset
current_lengthto 1 (start of a new interval). - By scanning through the entire sequence once, we obtain the length of the longest increasing interval.
Complexity
- Time complexity: \(O(N)\)
- Space complexity: \(O(1)\) (excluding input)
Implementation Notes
By initializing
max_length = 1, we guarantee that there is always at least 1 day.Since we look at each element only once, the
forloop starts fromi = 1.Note that we only advance the count when
A[i] > A[i - 1](the case where they are equal is not included).Source Code
N = int(input())
A = list(map(int, input().split()))
max_length = 1
current_length = 1
for i in range(1, N):
if A[i] > A[i - 1]:
current_length += 1
max_length = max(max_length, current_length)
else:
current_length = 1
print(max_length)
This editorial was generated by qwen3-coder-480b.
posted:
last update: