A - 合格者数の集計 / Counting the Number of Successful Applicants 解説 by admin
Gemini 3.0 FlashOverview
This is a problem where, among all students belonging to \(N\) classes, we need to find the total number of students whose scores are at or above the passing threshold \(K\).
Analysis
What this problem asks is, put very simply, to count “how many of all the given scores are \(K\) or above.”
At first glance, the number of students \(A_i\) differs for each class, and the input format might look a bit complex. However, it can essentially be solved with the following steps:
- Prepare a variable (counter) initialized to \(0\) to count the number of passing students.
- For each class \(i\), repeat the following operation \(N\) times:
- Check the number of students \(A_i\) in the class.
- Check each of the \(A_i\) students’ scores one by one, and if a score is \(K\) or above, increment the counter by \(1\).
- Output the final value of the counter.
Looking at the constraints, the total number of students (\(\sum A_i\)) is at most \(10^6\). A straightforward method that checks each score once (with time complexity \(O(\sum A_i)\)) will comfortably fit within the time limit.
Algorithm
Implement the following steps:
- Reading input:
In Python, using
sys.stdin.read().split()allows you to obtain all values as a single list, split by newlines and spaces. This makes it easy to handle input where the number of values per line varies. - Scanning the data:
- Retrieve the first two values (\(N, K\)).
- Then, for each class, read the number of students \(A_i\), and evaluate the next \(A_i\) values as scores.
- If a score is \(K\) or above, add \(1\) to the answer.
- Outputting the result: After checking all scores, output the total count.
Complexity
- Time complexity: \(O(N + \sum A_i)\) Since we check each score and each class’s student count exactly once, the computation finishes in time proportional to the total number of input values.
- Space complexity: \(O(N + \sum A_i)\)
Since all input is stored at once in a list (
tokens), memory usage is proportional to the total amount of input.
Implementation Notes
Handling variable-length arguments: Although the number of students differs by class, by using a variable to manage the list index (the
idxvariable in the code) and reading through sequentially, we can efficiently process everything as a single list without needing to create complex two-dimensional lists.Fast input: Rather than calling
input()multiple times, reading all input at once withsys.stdin.read().split()reduces execution time when the data volume is large.Source Code
import sys
def solve():
# 全ての入力を読み込み、スペースや改行で分割してトークンのリストを作成
data = sys.stdin.read().split()
if not data:
return
# 全てのトークンを整数に変換
tokens = list(map(int, data))
# 最初の2つのトークンはクラス数 N と合格ライン K
N = tokens[0]
K = tokens[1]
ans = 0
idx = 2
# 各クラスについて処理を繰り返す
for _ in range(N):
# tokens[idx] はそのクラスの人数 A_i
A_i = tokens[idx]
idx += 1
# そのクラスの生徒の点数 B_{i,j} を順にチェック
for _ in range(A_i):
if tokens[idx] >= K:
ans += 1
idx += 1
# 合計の合格者数を出力
print(ans)
if __name__ == '__main__':
solve()
This editorial was generated by gemini-3-flash-preview.
投稿日時:
最終更新: