A - テストの採点 / Test Grading 解説 by admin
Qwen3-Coder-480BOverview
A problem where, given the scores of students in each class, you need to find the number of students scoring at least a threshold \(K\) for each class.
Analysis
This problem is a straightforward counting problem — you simply check the scores of all students in each class and count those with a score of \(K\) or higher.
Looking at the constraints, \(N\) is at most \(10^5\), the number of students \(M_i\) in each class is at most \(100\), and the total number of students across all classes is at most \(10^6\).
While this is fairly large, iterating through all students is still within feasible limits (see the complexity analysis below).
A naive implementation (e.g., reading input line by line with input()) may result in TLE (Time Limit Exceeded) due to Python’s slow standard input.
Therefore, an optimization to read all input at once is necessary.
In this code, sys.stdin.read is used to read all input at once and process it as a list of strings. This avoids the overhead of repeated input calls and runs efficiently.
Algorithm
- Read all input at once and split it by whitespace into a list.
- Extract the first two values as \(N\) and \(K\).
- For each class, do the following:
- Get the number of students \(M_i\).
- Look at the next \(M_i\) scores in order and count those that are \(K\) or higher.
- Output the count for each class.
For example, given the following input:
2 70
3 80 60 90
4 50 70 80 60
- Class 1: Scores [80, 60, 90] → Number of students scoring 70 or higher: 2
- Class 2: Scores [50, 70, 80, 60] → Number of students scoring 70 or higher: 2
Thus the output is:
2
2
Complexity
- Time complexity: \(O(\sum_{i=1}^{N} M_i)\) (Since each student’s score is checked exactly once, it is proportional to the total number of students)
- Space complexity: \(O(\sum_{i=1}^{N} M_i)\) (Since the entire input data is stored in memory)
Implementation Notes
The key point is using
sys.stdin.readfor fast input.Since the input data is stored as a list of strings, values need to be converted with
int()as appropriate.When extracting data while managing an index, be careful not to access out of bounds.
Source Code
import sys
input = sys.stdin.read
def main():
data = input().split()
idx = 0
N = int(data[idx])
idx += 1
K = int(data[idx])
idx += 1
results = []
for _ in range(N):
M = int(data[idx])
idx += 1
count = 0
for _ in range(M):
score = int(data[idx])
idx += 1
if score >= K:
count += 1
results.append(count)
print('\n'.join(map(str, results)))
if __name__ == "__main__":
main()
This editorial was generated by qwen3-coder-480b.
投稿日時:
最終更新: