A - テストの採点 / Test Grading 解説 by admin
GPT 5.2 HighOverview
For each class, count and output the number of students whose scores are at least the threshold \(K\). We sequentially examine the input scores and count those that satisfy the condition.
Analysis
What we need to do in this problem is very simple: “count the number of scores that are \(K\) or above in each class’s score list.”
For example, if \(K=60\) and a class has scores \([70, 55, 60, 10]\), then the scores at least \(60\) are \(70\) and \(60\), giving 2 students, so the answer is 2.
The important point is that the input size is large. From the constraints, \(\sum M_i \le 10^6\), so we read up to 1 million scores.
With the following kind of implementation, Python tends to be slow:
- Calling
input()\(N\) times and performingsplit()each time for processing
→ The per-line processing overhead is large and can cause TLE.
The solution is to read all of standard input at once and process it efficiently as a sequence of integers.
There is no need to store the scores; it is sufficient to check whether each score is \(K\) or above at the moment it is read and increment the count, so no extra memory is used.
Algorithm
- Read the entire input at once using
sys.stdin.buffer.read() split()it into a sequence of integers and extract them sequentially as an iterator- First, read \(N\) and \(K\)
- For each class, do the following:
- Read the number of students \(M_i\)
- Read the next \(M_i\) scores, incrementing the count if the score is \(K\) or above
- Store the count result in an array
- Finally, output the result for each class joined by newlines
Complexity
- Time complexity: \(O\!\left(\sum_{i=1}^{N} M_i\right)\) (we just look at each score once)
- Space complexity: \(O(N)\) (for storing the answer for each class; additional space is needed for reading the input all at once)
Implementation Notes
Fast input: Using
sys.stdin.buffer.read().split()and processing integers sequentially via an iterator is fast.On-the-fly processing: Instead of creating an array of scores, checking
>= Kat the moment of reading and counting is simple and memory-efficient.Fast output: Accumulating results as strings in
resand outputting them all at once with"\n".join(res)at the end is efficient.Source Code
import sys
def main():
it = iter(map(int, sys.stdin.buffer.read().split()))
N = next(it)
K = next(it)
res = []
for _ in range(N):
m = next(it)
cnt = 0
for _ in range(m):
if next(it) >= K:
cnt += 1
res.append(str(cnt))
sys.stdout.write("\n".join(res))
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.2-high.
投稿日時:
最終更新: