A - テストの点差 / Test Score Difference 解説 by admin
gpt-5.3-codexOverview
For each student, calculate “how many points higher than Takahashi,” then find the total.
Specifically, the answer is obtained by summing \(\max(0, A_i - X)\) for each \(A_i\).
Analysis
The definition of frustration is as follows:
- If \(A_i > X\), then \(A_i - X\)
- Otherwise, \(0\)
In other words, each student’s contribution is the same as \(\max(0, A_i - X)\).
Therefore, the total frustration is given by
\[ \sum_{i=1}^{N} \max(0, A_i - X) \]
Key Insight
Each student’s frustration is independent of other students.
Therefore, the total can be computed by simply scanning the array once from left to right (just accumulating).
On the Naive Approach
For this problem, a full scan (looking at each person in order) is itself the optimal approach.
Unnecessary sorting or nested loops are not needed and would only increase the computational complexity.
Since \(N \le 5 \times 10^5\), a single-pass \(O(N)\) solution is safe.
For example, when \(X=70\), \(A=[65, 80, 70, 90]\):
- 65 is not higher, so 0
- 80 gives \(80-70=10\)
- 70 is a tie, so 0
- 90 gives \(90-70=20\)
The total is \(0+10+0+20=30\).
Algorithm
- Read \(N, X\).
- Read the score sequence \(A\).
- Initialize
ans = 0. - For each
a:- If
a > X, thenans += a - X - Otherwise, do nothing
- If
- Output
ans.
The provided code implements this procedure directly.
Complexity
- Time complexity: \(O(N)\)
- Space complexity: \(O(1)\) (the input array is processed sequentially using
map, so additional memory is constant)
Implementation Notes
The condition is “only when strictly higher,” so note that it is
a > X(not>=).Using
sys.stdin.readlineallows fast processing even with large inputs.By writing
A = map(int, input().split()), you can process elements sequentially without creating an array.Source Code
import sys
def main():
input = sys.stdin.readline
N, X = map(int, input().split())
A = map(int, input().split())
ans = 0
for a in A:
if a > X:
ans += a - X
print(ans)
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.3-codex.