公式

A - 連続する高得点区間 / Consecutive High-Score Segments 解説 by admin

Gemini 3.0 Flash

Overview

This problem asks you to count how many contiguous intervals (groups) exist among the given \(N\) scores where the values are at or above the passing score \(K\).

Analysis

The key to solving this problem is to accurately identify “when a new ceremony group begins.”

For example, suppose the passing score is \(80\) and the scores are arranged as \([90, 85, 50, 80, 40]\). 1. The first person with \(90\) points passes. A new group starts here (count: 1). 2. The next person with \(85\) points also passes, but since the previous person also passed, they belong to the same group (the count does not increase). 3. The next person with \(50\) points fails. The group is interrupted here. 4. The next person with \(80\) points passes. Since the previous person failed, a new group starts here (count: 2). 5. The last person with \(40\) points fails.

In this way, by incrementing the count at “the moment a passing student appears for the first time after a failing student (or at the beginning of the sequence),” we can determine the total number of groups.

Algorithm

We prepare a flag (variable) to track “whether we are currently inside a group of passing students,” and check the scores sequentially from the beginning.

  1. Initialize a variable count representing the number of ceremonies to \(0\).
  2. Initialize a flag in_group representing “whether we are currently inside a group” to False.
  3. For each student’s score score, repeat the following process:
    • If score is \(K\) or above (passing):
      • If we are not yet inside a group (i.e., in_group is False), this is the start of a new group, so increment count by \(1\) and set in_group to True.
      • If we are already inside a group, do nothing.
    • If score is below \(K\) (failing):
      • The group is interrupted, so set in_group to False.
  4. Output the final value of count.

Complexity

  • Time complexity: \(O(N)\) For \(N\) students, we only scan through the list of scores once from start to end, so it runs very efficiently.
  • Space complexity: \(O(N)\) We use \(O(N)\) memory to store the input as a list.

Implementation Notes

  • Flag toggling: The implementation uses simple logic — “count and raise the flag” when the score is at or above the passing score, and “lower the flag” when it is below.

  • Large input: Since \(N\) can be as large as \(2 \times 10^5\), in Python you can reduce execution time by reading all input at once using methods like sys.stdin.read().split().

    Source Code

import sys

def solve():
    # 入力を受け取る
    input_data = sys.stdin.read().split()
    if not input_data:
        return
    
    n = int(input_data[0])
    k = int(input_data[1])
    a = list(map(int, input_data[2:]))

    count = 0
    in_group = False
    
    # 各生徒の点数を確認していく
    for score in a:
        if score >= k:
            # 合格点以上かつ、現在グループのカウント中でなければ新しいグループを開始
            if not in_group:
                count += 1
                in_group = True
        else:
            # 合格点未満なら、連続したグループが途切れたことを示す
            in_group = False
            
    # 結果を出力
    print(count)

if __name__ == "__main__":
    solve()

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

投稿日時:
最終更新: