D - 突出したスコア / Outstanding Score Editorial by admin
GPT 5.2 HighOverview
For each student, determine whether “their score is at least \(K\) times the average score of the other students,” and count the number of students who satisfy this condition.
Analysis
The condition is “student \(i\)’s score \(A_i\) is at least \(K\) times the average of everyone else’s scores.” Using averages introduces division. However, the problem statement provides an equivalent inequality:
\(A_i \times (N - 1) \geq K \times (S - A_i)\) (where \(S=\sum_{j=1}^N A_j\))
This allows us to check the condition using only integer arithmetic without division.
If we naively compute “the sum of everyone except student \(i\)” each time, it takes \(O(N)\) per student and \(O(N^2)\) overall, which is too slow for \(N \le 2\times 10^5\) (TLE).
Instead, by precomputing the total sum \(S\) in \(O(N)\), we can instantly obtain the sum excluding student \(i\) as \(S-A_i\). This makes each student’s check \(O(1)\), and the entire solution runs in \(O(N)\).
Also, when \(N=1\), both sides of the inequality are always \(0\), so the sole student always satisfies the condition (as noted in the problem statement).
Algorithm
- Read input \(N, K\) and array \(A\).
- If \(N=1\), the answer is \(1\).
- Compute the total \(S=\sum A_i\).
- For each student \(i\), check the following condition and increment the count if true:
- \(A_i \times (N-1) \ge K \times (S-A_i)\)
- Output the count.
(Example) When \(N=3, K=2, A=[10,3,2]\), we have \(S=15\).
- \(i=1\): \(10\times 2 \ge 2\times (15-10)=10\) → \(20\ge 10\), true
- \(i=2\): \(3\times 2 \ge 2\times 12\) → \(6\ge 24\), false
- \(i=3\): \(2\times 2 \ge 2\times 13\) → \(4\ge 26\), false
Therefore the answer is \(1\).
Complexity
- Time complexity: \(O(N)\) (sum computation \(O(N)\) + checking each element \(O(N)\))
- Space complexity: \(O(N)\) (storing array \(A\))
Implementation Notes
It is safer to use the given inequality \(A_i (N-1) \ge K(S-A_i)\) directly rather than computing the average with division.
Values can be up to \(10^9\), and the total \(S\) can be as large as \(2\times 10^5 \times 10^9 = 2\times 10^{14}\). In Python, integers have arbitrary precision so there is no issue (in other languages, use 64-bit integers).
Since the input can be large, using fast input methods like
sys.stdin.buffer.read()helps ensure stability.Source Code
import sys
def main():
data = sys.stdin.buffer.read().split()
N = int(data[0])
K = int(data[1])
A = list(map(int, data[2:2+N]))
if N == 1:
print(1)
return
S = sum(A)
n1 = N - 1
ans = 0
for ai in A:
if ai * n1 >= K * (S - ai):
ans += 1
print(ans)
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.2-high.