A - テストの採点 / Test Grading 解説 by admin
gemini-3-flash-previewOverview
This problem requires checking the scores of \(M_i\) students in each of \(N\) classes and counting how many students scored at or above the threshold \(K\).
Analysis
The key point of this problem lies in handling the total data volume and input format.
- Regarding computational complexity: The total number of students \(\sum M_i\) can reach up to \(10^6\). If we take the straightforward approach of checking each student’s score once and comparing it with the threshold \(K\), the total number of operations is proportional to the total number of students. This is a computational complexity that can comfortably finish within the time limit.
- Regarding input handling: Each line contains “the number of students \(M_i\)” followed by “\(M_i\) scores.” Since the number of values varies per line, it is necessary to either read and process one line at a time, or read all values into a list beforehand and process them sequentially.
Algorithm
We proceed with the following steps:
- Reading the data: For efficiency, read all values from standard input at once and convert them into a list of integers.
- Processing each class:
While managing the current read position (index), repeat the following operations \(N\) times:
- Obtain the number of students \(M_i\) for that class.
- Check the subsequent \(M_i\) scores one by one, and increment the count by 1 if the value is at least \(K\).
- Record the count result and advance the read position to the beginning of the next class’s data.
- Outputting the results: Output the count results for each class, separated by newlines.
Complexity
- Time complexity: \(O(N + \sum M_i)\) Since we process each class once and check each student’s score once, the processing completes in time proportional to the total input size.
- Space complexity: \(O(N + \sum M_i)\) When storing all input in a list, the memory usage is proportional to the data volume.
Implementation Notes
Fast I/O: In Python, calling
input()many times when the number of students is large can lead to long execution times. By usingsys.stdin.read().split()to read everything at once andsys.stdout.write()for bulk output at the end, execution speed can be significantly improved.Index management: Since we are dealing with variable-length data, managing the position of the next data to read with a variable (e.g.,
idx) allows flexible handling of complex input formats.Source Code
import sys
def solve():
# 標準入力からすべてのトークンを読み込み、整数のリストに変換します
# sys.stdin.read().split() は空白(スペース、改行、タブ)で分割されたすべての単語をリストとして取得します
input_data = sys.stdin.read().split()
if not input_data:
return
# mapを使用して一括で整数に変換することで、効率的に処理します
data = list(map(int, input_data))
# クラス数 N と基準点 K を取得
N = data[0]
K = data[1]
# データの読み取り位置を管理するインデックス
idx = 2
# 各クラスの結果を格納するリスト
results = []
# N 個のクラスについて順番に処理を行います
for _ in range(N):
# クラス i の生徒数 M_i
m = data[idx]
idx += 1
# クラス i の点数リストをスライスで取得し、K点以上の人数をカウントします
# スライスとループの組み合わせはPythonにおいて効率的です
count = 0
class_scores = data[idx : idx + m]
for score in class_scores:
if score >= K:
count += 1
# カウントした結果を文字列としてリストに追加
results.append(str(count))
# インデックスを次のクラスのデータ位置まで進める
idx += m
# すべての結果を改行で連結して一気に出力します
sys.stdout.write('\n'.join(results) + '\n')
if __name__ == '__main__':
solve()
This editorial was generated by gemini-3-flash-preview.
投稿日時:
最終更新: