A - 合格者数の集計 / Counting the Number of Successful Applicants 解説 by admin
GPT 5.2 HighOverview
Read the scores of all students in all classes, count the number of students scoring at or above the passing threshold \(K\), and output the total.
Analysis
What needs to be done in this problem is very straightforward: for each score \(B_{i,j}\), determine whether it is \(K\) or greater, and sum up the count of those that satisfy the condition. Even if you tally by class, what we ultimately want is the overall total, so it suffices to look at each score exactly once.
However, given the constraint \(\sum A_i \le 10^6\) (up to 1 million students’ scores), the input size can be large. A naive implementation such as:
- Calling
input()\(N\) times - Splitting each line with
split()and converting to integers
may pass, but in Python the overhead of input processing is significant, and depending on the environment, this can easily result in TLE (Time Limit Exceeded).
Therefore, the correct solution uses sys.stdin.buffer.read() to read the entire input as a byte string at once, and then uses a custom parser (read_int()) to quickly read integers from it. This allows stable and fast processing even when handling on the order of 1 million integers.
(Example) If \(K=60\) and the scores are [55, 60, 100], then \(60\) and \(100\) are the 2 passing students, so we add +2. We simply repeat this for all classes.
Algorithm
- Read \(N, K\).
- Initialize the answer
ans = 0. - For each class \(i=1..N\):
- Read the number of students \(A_i\).
- Read \(A_i\) scores one by one; if a score is \(K\) or greater, do
ans += 1.
- Output
ans.
The key point is “not storing the scores.” Since all we need is the number of passing students, counting while reading (incremental processing) is sufficient.
Complexity
- Time complexity: \(O\!\left(\sum_{i=1}^{N} A_i\right)\) (we look at each student’s score exactly once)
- Space complexity: \(O(1)\) (since we do not store scores in an array, additional memory is constant)
※ However, in practice,
buffer.read()holds the entire input, so memory proportional to the input size is used.
Implementation Notes
- Since the input can contain up to \(10^6\) scores, fast input is crucial in Python. By using
sys.stdin.buffer.read()combined with a customread_int(), we skip whitespace and newlines while parsing integers quickly. - The implementation of
read_int()works with ASCII codes:- Skip whitespace and newlines (\(\le 32\))
- Build the integer from digit characters (\(>32\)) using
num = num*10 + digit
This approach reduces the number of calls to split() and int().
- Scores range from \(0..100\) and \(K\) also ranges from \(0..100\), so a simple comparison score >= K is sufficient.
Source Code
import sys
data = sys.stdin.buffer.read()
L = len(data)
idx = 0
def read_int():
global idx
while idx < L and data[idx] <= 32:
idx += 1
num = 0
while idx < L and data[idx] > 32:
num = num * 10 + (data[idx] - 48)
idx += 1
return num
N = read_int()
K = read_int()
ans = 0
for _ in range(N):
A = read_int()
for _ in range(A):
if read_int() >= K:
ans += 1
sys.stdout.write(str(ans) + "\n")
This editorial was generated by gpt-5.2-high.
投稿日時:
最終更新: