A - 連続する高得点区間 / Consecutive High-Score Segments Editorial by admin
GPT 5.2 HighOverview
Count the number of “contiguous segments” in the sequence where students have scores of \(K\) or more (passing students). This count equals the required number of award ceremonies.
Analysis
One award ceremony suffices for each “group of passing students with consecutive student numbers.” In other words, what we want to find is, when looking at the array \(A_1,\dots,A_N\) from left to right:
- Treat consecutive passes (\(A_i \ge K\)) as a single segment
- Count how many such segments exist
This is the “number of contiguous True segments.”
For example, if \(K=60\) and the pass/fail results (T for pass) are:
F T T F T F T T T, there are 3 consecutive T blocks, so the answer is 3.
A naive approach like “enumerating all segments of passing students” could be \(O(N^2)\), which is too slow for \(N \le 2\times 10^5\). However, this problem only requires knowing whether each position \(i\) is “the start of a new passing segment,” which can be counted in a single scan.
The key observation is:
- If position \(i\) is a pass and the previous position \(i-1\) is a fail (or \(i=1\)), then this is the start of a new passing segment
- Otherwise (just a continuation of passes), the number of ceremonies does not increase
Algorithm
- Prepare
ans = 0(number of ceremonies) andprev_ok = False(whether the previous position was a pass). - Read scores \(A_i\) sequentially for \(i=1\) to \(N\).
- Set
ok = (A_i >= K). - If
okis true andprev_okis false, this is the “start of a passing segment,” soans += 1. - Update
prev_ok = okand proceed to the next. - Output
ansat the end.
This counts the number of passing segment starts = number of passing segments = number of award ceremonies.
Complexity
- Time complexity: \(O(N)\) (just a single scan)
- Space complexity: \(O(1)\) (only a constant number of additional variables)
Implementation Notes
The essential condition is “if passing and the previous was not passing, then +1” (the boundary case \(i=1\) is naturally handled by initializing
prev_ok=False).Since the input can be large, in Python it is safer to use fast input methods like
sys.stdin.buffer.read().Even when there are no passing students at all, this counting method naturally keeps
ans=0.Source Code
import sys
def main():
it = iter(sys.stdin.buffer.read().split())
N = int(next(it))
K = int(next(it))
ans = 0
prev_ok = False
for _ in range(N):
a = int(next(it))
ok = a >= K
if ok and not prev_ok:
ans += 1
prev_ok = ok
print(ans)
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.2-high.
posted:
last update: