A - テストの点差 / Test Score Difference 解説 by admin 印刷ページ
Claude 4.6 Opus (Thinking)Overview
This is a problem where you calculate the difference in scores for each student who scored higher than Takahashi, and find the total sum.
Analysis
In this problem, you simply calculate the following value for each student \(i\) and sum them all up.
- When \(A_i > X\): the frustration is \(A_i - X\)
- When \(A_i \leq X\): the frustration is \(0\)
Let’s think through a concrete example.
For instance, if \(N = 4\), \(X = 60\) (Takahashi’s score), and the students’ scores are \([80, 50, 90, 60]\):
| Student | Score \(A_i\) | \(A_i > X\)? | Frustration |
|---|---|---|---|
| 1 | 80 | \(80 > 60\) → Yes | \(80 - 60 = 20\) |
| 2 | 50 | \(50 > 60\) → No | \(0\) |
| 3 | 90 | \(90 > 60\) → Yes | \(90 - 60 = 30\) |
| 4 | 60 | \(60 > 60\) → No | \(0\) |
The total frustration is \(20 + 0 + 30 + 0 = 50\).
Since this problem can be solved with a simple loop, no special techniques are needed. Although \(N\) can be up to \(5 \times 10^5\), a single loop is more than sufficient.
Algorithm
- Read \(N\) (number of students) and \(X\) (Takahashi’s score).
- Read the scores \(A_1, A_2, \ldots, A_N\) of the \(N\) students.
- For each student, if \(A_i > X\), add \(A_i - X\) to the total.
- Output the total.
In the code, we use a Python generator expression to write it concisely in one line:
sum(a - X for a in A if a > X)
This means “for each element \(a\) in list \(A\), select only those where \(a > X\), compute \(a - X\), and sum them up.”
Complexity
- Time complexity: \(O(N)\) — we only look at each student once
- Space complexity: \(O(N)\) — for storing the students’ scores in a list
Implementation Notes
Note that the condition \(A_i > X\) uses a strict inequality (not \(\geq\)). When \(A_i = X\), the frustration is \(0\).
By combining Python’s
sumwith a generator expression, you can omit explicit loops and variable initialization, resulting in concise code.Source Code
N, X = map(int, input().split())
A = list(map(int, input().split()))
print(sum(a - X for a in A if a > X))
This editorial was generated by claude4.6opus-thinking.
投稿日時:
最終更新: