D - 突出したスコア / Outstanding Score Editorial by admin Editorial for printing
Qwen3-Coder-480BOverview
Given each student’s score, find the number of students whose score is “at least \(K\) times the average of everyone else’s scores.”
Analysis
First, let’s organize the condition given in the problem.
The condition for student \(i\) to be “outstanding” is expressed by the following inequality: $\( A_i \times (N - 1) \geq K \times (S - A_i) \)\( where \)S = A_1 + A_2 + \cdots + A_N$, i.e., the total score of all students.
By transforming the expression this way, we can easily check the condition for each student.
The left side is \(A_i \times (N - 1)\) and the right side is \(K \times (S - A_i)\), so we simply perform this comparison for each student.
What about a naive approach?
If we recalculated the average of the other \(N-1\) students for each student, the total time complexity would be \(O(N^2)\), and since \(N\) can be up to \(2 \times 10^5\), this would not be fast enough (TLE).
Improvement
However, we don’t actually need to compute the average each time. If we precompute the total sum \(S\), then \(S - A_i\) gives us the “sum of everyone else’s scores.” Using this, we can check the condition for each student in constant time.
Therefore, the entire problem can be solved in \(O(N)\).
Algorithm
- First, compute the total sum \(S\) of all students’ scores.
- For each student \(i\), check the following inequality: $\( A_i \times (N - 1) \geq K \times (S - A_i) \)$
- Count and output the number of students satisfying the condition.
Complexity
- Time complexity: \(O(N)\)
- Space complexity: \(O(1)\) (excluding input)
Implementation Notes
Compute the total \(S\) only once at the beginning.
Be careful about overflow when evaluating the inequality, though this is not an issue in Python.
The formula works correctly even when \(N = 1\) (both sides become 0, so the condition is always satisfied).
Source Code
N, K = map(int, input().split())
A = list(map(int, input().split()))
S = sum(A)
count = 0
for Ai in A:
if Ai * (N - 1) >= K * (S - Ai):
count += 1
print(count)
This editorial was generated by qwen3-coder-480b.
posted:
last update: