Official

A - 連続晴天の最長記録 / Longest Streak of Hot Days Editorial by admin

gemini-3-flash-thinking

Overview

Given \(N\) days of temperature data, the problem asks you to find the maximum number of consecutive days where the temperature was \(K\) or higher (i.e., consecutive “extreme heat days”).

Analysis

The key idea in this problem is: “The moment the streak of extreme heat days is broken, reset the consecutive count back to zero.”

For example, consider temperatures [30, 32, 25, 31, 33, 34, 28] with \(K=30\): 1. Day 1 (30) and Day 2 (32) are \(K\) or higher, so the consecutive count becomes 2. 2. Day 3 (25) is below \(K\), so the streak is broken here. Reset the consecutive count to 0. 3. Days 4 through 6 are \(K\) or higher, so the consecutive count extends to 3. 4. Day 7 (28) breaks the streak again.

Among all the consecutive counts recorded during this process, the maximum value of 3 is the answer.

Since \(N\) can be as large as \(10^6\), using a double loop to try “all combinations of start and end points” would result in \(O(N^2)\) time complexity, which would exceed the time limit (TLE). An efficient \(O(N)\) approach that scans the data from the beginning only once is required.

Algorithm

  1. Initialize a variable max_streak to hold the maximum consecutive days and a variable current_streak to hold the current consecutive days, both set to \(0\).
  2. Examine the temperature data sequentially from Day 1.
    • If the temperature on that day is \(K\) or higher, increment current_streak by \(1\).
    • After incrementing, if current_streak is greater than max_streak, update max_streak.
    • If the temperature on that day is below \(K\), it means the streak has been broken, so reset current_streak to \(0\).
  3. Output the value of max_streak after all days have been examined.

Complexity

  • Time Complexity: \(O(N)\)
    • Since the data is scanned only once from beginning to end, the processing completes in time proportional to the number of days \(N\).
  • Space Complexity: \(O(N)\)
    • Memory proportional to \(N\) is used to store the input temperature data as a list.

Implementation Notes

  • Fast I/O: Since \(N\) can be as large as \(10^6\), reading all input at once using sys.stdin.read().split() is faster than repeatedly calling Python’s standard input(), which helps reduce execution time.

  • Timing of Maximum Value Updates: By always updating max_streak while the streak of extreme heat days continues, the calculation remains correct even when the data ends in the middle of a streak of extreme heat days.

    Source Code

import sys

def main():
    # 入力を一括で読み込み、スペース区切りで分割します
    input_data = sys.stdin.read().split()
    
    if not input_data:
        return
    
    # 日数 N と基準気温 K を取得
    n = int(input_data[0])
    k = int(input_data[1])
    
    # 各日の気温 A_i を取得(イテレータとして扱うことでメモリ効率を上げます)
    temperatures = map(int, input_data[2:])
    
    max_streak = 0
    current_streak = 0
    
    for temp in temperatures:
        if temp >= k:
            # 猛暑日の場合、現在の連続日数をインクリメント
            current_streak += 1
            # 最大値を更新
            if current_streak > max_streak:
                max_streak = current_streak
        else:
            # 非猛暑日の場合、連続日数をリセット
            current_streak = 0
            
    # 結果を出力
    print(max_streak)

if __name__ == '__main__':
    main()

This editorial was generated by gemini-3-flash-thinking.

posted:
last update: