A - 合格者数の集計 / Counting the Number of Successful Applicants 解説 by admin
Claude 4.5 OpusOverview
This is a problem where you need to examine the scores of all students in all classes and count how many students scored at or above the passing threshold of \(K\) points.
Analysis
This problem is a simple aggregation problem where you check all given scores and count those that satisfy the condition.
Key Observations
- Understanding the input format: Each line starts with the number of students \(A_i\), followed by \(A_i\) scores
- Check all students without omission: Process all classes in order and check the scores of all students in each class
- Judgment condition: Since the score must be “\(K\) points or higher”, we check whether \(B_{i,j} \geq K\) is satisfied
Is a Naive Approach Sufficient?
- The total number of students is at most \(10^6\)
- Checking whether each student scored \(K\) or higher can be done in \(O(1)\)
- Therefore, simply checking all students is efficient enough
However, care must be taken with the input reading method. In Python, calling input() line by line can be slow for large amounts of input.
Algorithm
- First, read \(N\) (number of classes) and \(K\) (passing threshold)
- Initialize a variable
countto 0 to count the number of passing students - For each class \(i\) (\(i = 1, 2, \ldots, N\)):
- Read the number of students \(A_i\) in that class
- Read the scores for \(A_i\) students in sequence
- For each score \(B_{i,j}\), if \(B_{i,j} \geq K\), increment
countby 1
- Output the final
count
Concrete Example
Sample input:
2 60
3 55 70 80
2 59 60
- Class 1: 3 students with scores 55, 70, 80 → 2 students scored 60 or higher: 70, 80
- Class 2: 2 students with scores 59, 60 → 1 student scored 60 or higher: 60
- Total: \(2 + 1 = 3\) students
Complexity
- Time complexity: \(O\left(\sum_{i=1}^{N} A_i\right)\)
- Because we check each student’s score once
- At most \(10^6\) operations
- Space complexity: \(O\left(\sum_{i=1}^{N} A_i\right)\)
- Because we read the input data all at once
- Can be reduced to \(O(\max A_i)\) if processed line by line
Implementation Notes
Fast input processing: By reading all input at once with
sys.stdin.read()and splitting withsplit(), you can process large amounts of input efficientlyIndex management: Keep the input data as an array and manage the current reading position with an
idxvariable. This allows flexible processing of each line’s structure (number of students first, then scores)Counting method: By immediately judging and counting each score as it’s read, there’s no need to store all scores (memory efficient)
Source Code
import sys
def main():
input_data = sys.stdin.read().split()
idx = 0
N = int(input_data[idx])
idx += 1
K = int(input_data[idx])
idx += 1
count = 0
for _ in range(N):
A = int(input_data[idx])
idx += 1
for _ in range(A):
B = int(input_data[idx])
idx += 1
if B >= K:
count += 1
print(count)
if __name__ == "__main__":
main()
This editorial was generated by claude4.5opus.
投稿日時:
最終更新: