A - 合格基準 / Passing Criteria 解説 by admin
GPT 5.2 HighOverview
This is a problem where we count the number of students who “pass” based on the condition that their score differs from a reference score \(S\) by at most \(T\) (i.e., \(|A_i-S|\le T\)).
Analysis
For each student, whether they “pass or not” can be determined independently based solely on \(A_i\).
Therefore, it is sufficient to examine each student’s score once and count the number of people satisfying the condition \(|A_i-S|\le T\).
For example, when \(S=50, T=5\):
- \(A_i=47\): \(|47-50|=3 \le 5\), so they pass
- \(A_i=60\): \(|60-50|=10 > 5\), so they fail
In this way, we simply check the absolute value condition as-is.
The constraint is \(N \le 2\times 10^5\), so a single \(O(N)\) scan is more than sufficient (conversely, unnecessarily heavy operations such as checking all combinations would not finish in time).
Algorithm
- Read \(N,S,T\) and the array \(A\) as input.
- Initialize a counter
ans=0. - For each score \(A_i\), if \(|A_i-S|\le T\), increment
ansby 1. - Output
ansat the end.
In the code, we use sum(1 for x in A if abs(x - S) <= T) to count the number of elements satisfying the condition all at once.
Complexity
- Time complexity: \(O(N)\) (checking each student exactly once)
- Space complexity: \(O(N)\) (to store the score sequence \(A\). This can be reduced to \(O(1)\) with an implementation that counts while reading)
Implementation Notes
The condition must be written directly using absolute value as \(|A_i-S|\le T\) (
abs(A_i - S) <= T).Since the input can have up to \(2\times 10^5\) elements, in Python it is stable and fast to read all input at once using
sys.stdin.buffer.read().Source Code
import sys
def main():
data = list(map(int, sys.stdin.buffer.read().split()))
N, S, T = data[0], data[1], data[2]
A = data[3:3+N]
ans = sum(1 for x in A if abs(x - S) <= T)
print(ans)
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.2-high.
投稿日時:
最終更新: