A - 連続する高得点区間 / Consecutive High-Score Segments 解説 by admin
Gemini 3.0 FlashOverview
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.
- Initialize a variable
countrepresenting the number of ceremonies to \(0\). - Initialize a flag
in_grouprepresenting “whether we are currently inside a group” toFalse. - For each student’s score
score, repeat the following process:- If
scoreis \(K\) or above (passing):- If we are not yet inside a group (i.e.,
in_groupisFalse), this is the start of a new group, so incrementcountby \(1\) and setin_grouptoTrue. - If we are already inside a group, do nothing.
- If we are not yet inside a group (i.e.,
- If
scoreis below \(K\) (failing):- The group is interrupted, so set
in_grouptoFalse.
- The group is interrupted, so set
- If
- 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.
投稿日時:
最終更新: