A - 連続上昇気温 / Consecutive Rising Temperatures Editorial by admin
GPT 5.2 HighOverview
Given a temperature sequence \(A_1,\dots,A_N\), find the length of the longest contiguous subsequence that is strictly increasing.
Analysis
An “ascending period” is a contiguous interval where the condition \(A_{i-1} < A_i\) holds throughout the entire interval. In other words, by looking at each pair of adjacent days and checking whether the value is increasing or not, we can determine whether to extend the current ascending period or reset it.
Naively checking all intervals \((l,r)\) would require examining \(O(N^2)\) intervals, and checking whether each interval is increasing would take even more time, which is certainly too slow (TLE) for \(N \le 2\times 10^5\).
Instead, by scanning from left to right in a single pass:
- If the current value is greater than the previous one, increment the current consecutive increasing length cur by \(+1\)
- Otherwise, reset cur=1
This allows us to simultaneously find the maximum length best.
Example: \([3, 5, 4, 6, 7]\)
- \(3 \to 5\) is increasing: cur=2
- \(5 \to 4\) is not increasing: cur=1
- \(4 \to 6\) is increasing: cur=2
- \(6 \to 7\) is increasing: cur=3
Therefore the answer is \(3\) (\(4,6,7\)).
Algorithm
- Initialize
best=1,cur=1(even when \(N=1\), the answer is 1). - Iterate from day \(i=2\) to day \(N\) (in array indexing,
i=1..N-1). - If \(A_i > A_{i-1}\), the consecutive increase continues, so
cur += 1. - Otherwise, the increase is broken, so
cur = 1. - At each step, update
best = max(best, cur). - Output
best.
Complexity
- Time complexity: \(O(N)\) (only a single pass)
- Space complexity: \(O(1)\) (only a constant number of variables besides the input array)
Implementation Notes
Since we require strictly increasing, the condition is \(A_i > A_{i-1}\) (not \(A_i \ge A_{i-1}\)) — be careful about this.
To correctly return an answer of \(1\) when \(N=1\), we start with
best=cur=1.Since the input can be large, using
sys.stdin.buffer.read()in Python allows for faster input reading.Source Code
import sys
def main():
data = list(map(int, sys.stdin.buffer.read().split()))
if not data:
return
n = data[0]
a = data[1:1+n]
best = cur = 1
for i in range(1, n):
if a[i] > a[i-1]:
cur += 1
else:
cur = 1
if cur > best:
best = cur
print(best)
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.2-high.
posted:
last update: