公式

A - テストの点差 / Test Score Difference 解説 by admin 印刷ページ

GPT 5.4 High

Overview

For each student, you simply follow the rule: “if their score is higher than Takahashi’s, add the difference; otherwise, add \(0\),” and sum everything up.

Analysis

The frustration for student \(i\) is defined exactly as stated in the problem:

  • If \(A_i > X\), then \(A_i - X\)
  • If \(A_i \le X\), then \(0\)

Therefore, summing this value over all students gives the answer.

For example, if \(X = 60\) and the students’ scores are \([55, 80, 60, 75]\):

  • Student with \(55\) points: not higher than Takahashi, so \(0\)
  • Student with \(80\) points: \(80 - 60 = 20\)
  • Student with \(60\) points: tied, so \(0\)
  • Student with \(75\) points: \(75 - 60 = 15\)

The total is \(0 + 20 + 0 + 15 = 35\).

In this problem, it is sufficient to look at each student exactly once.
Since \(N \le 5 \times 10^5\), an \(O(N)\) approach that checks each student in order is fast enough.

Conversely, adding unnecessary complexity or recalculating for each student multiple times would be wasteful. For this problem, implementing it straightforwardly according to the definition is the simplest and most reliable approach.

Algorithm

  1. Initialize a variable ans to \(0\) to represent the answer.
  2. For each student’s score \(a\), do the following:
    • If \(a > X\), then ans += a - X
    • Otherwise, do nothing
  3. Finally, output ans.

This computes the total frustration over all students.

Complexity

  • Time complexity: \(O(N)\)
  • Space complexity: \(O(1)\)

Implementation Notes

  • The condition only applies “when the score is higher,” so writing if a > X: is sufficient.

  • When the scores are tied, the frustration is \(0\), so note that it should be a > X and not a >= X.

  • The provided code uses sys.stdin.buffer.read() for fast input, but the actual processing is simply a single pass through the data.

    Source Code

import sys

data = list(map(int, sys.stdin.buffer.read().split()))
N, X = data[0], data[1]
ans = 0
for a in data[2:2 + N]:
    if a > X:
        ans += a - X
print(ans)

This editorial was generated by gpt-5.4-high.

投稿日時:
最終更新: