Official

D - 突出したスコア / Outstanding Score Editorial by admin Editorial for printing

Claude 4.6 Opus (Thinking)

Overview

For each of the \(N\) students, determine whether that student’s score is “at least \(K\) times the average score of all other students,” and count the number of students satisfying this condition.

Analysis

Reformulating the Condition

The condition for student \(i\) to be “outstanding” is, letting \(\overline{A}_{-i}\) denote the average score of everyone except themselves:

\[A_i \geq K \times \overline{A}_{-i}\]

Since the average score excluding the student is \(\frac{S - A_i}{N - 1}\) (where \(S = A_1 + A_2 + \cdots + A_N\)), we can multiply both sides by \(N-1\) to avoid division:

\[A_i \times (N - 1) \geq K \times (S - A_i)\]

This reduces the problem to a comparison involving only integers.

Concrete Example

For example, with \(N = 4\), \(K = 3\), \(A = [10, 2, 3, 1]\), we have \(S = 16\).

  • Student 1 (\(A_1 = 10\)): Compare \(10 \times 3 = 30\) with \(3 \times (16 - 10) = 18\) → \(30 \geq 18\) ✅ Outstanding
  • Student 2 (\(A_2 = 2\)): Compare \(2 \times 3 = 6\) with \(3 \times (16 - 2) = 42\) → \(6 \geq 42\) ❌
  • Student 3 (\(A_3 = 3\)): Compare \(3 \times 3 = 9\) with \(3 \times (16 - 3) = 39\) → \(9 \geq 39\) ❌
  • Student 4 (\(A_4 = 1\)): Compare \(1 \times 3 = 3\) with \(3 \times (16 - 1) = 45\) → \(3 \geq 45\) ❌

Therefore, the answer is 1.

Comparison with the Naive Approach

If we recompute the “sum excluding the current student” from scratch for each student, the complexity becomes \(O(N^2)\). However, by precomputing the total sum \(S\), we can obtain \(S - A_i\) in \(O(1)\).

Avoiding Floating-Point Numbers

Computing the average through division risks floating-point errors. By transforming the expression into the integer comparison \(A_i \times (N-1) \geq K \times (S - A_i)\), we can make the determination accurately without any errors.

Algorithm

  1. Read \(N\), \(K\), and the array \(A\).
  2. Compute the total sum \(S = A_1 + A_2 + \cdots + A_N\).
  3. For each student \(i\), check whether \(A_i \times (N - 1) \geq K \times (S - A_i)\).
  4. Output the number of students satisfying the condition.

Complexity

  • Time complexity: \(O(N)\) (\(O(N)\) for computing the sum, \(O(N)\) for checking each student)
  • Space complexity: \(O(N)\) (for storing the array \(A\))

Implementation Notes

  • Avoid division: Instead of computing the average as a floating-point number, multiply both sides by \((N-1)\) to perform an integer comparison, preventing errors.

  • Watch out for overflow: Since \(A_i\), \(K\), and \(S\) can be up to around \(10^9\) and \(N\) can be up to \(2 \times 10^5\), the result of multiplication can reach approximately \(10^{18}\). In C++ and similar languages, long long is required. In Python, this is not an issue since arbitrary-precision integers are supported by default.

  • Case \(N = 1\): Both sides of the equation become \(0\), so the condition always holds. No special case handling is needed.

    Source Code

N, K = map(int, input().split())
A = list(map(int, input().split()))
S = sum(A)
count = 0
for a in A:
    if a * (N - 1) >= K * (S - a):
        count += 1
print(count)

This editorial was generated by claude4.6opus-thinking.

posted:
last update: