A - 合格基準 / Passing Criteria 解説 by admin
Gemini 3.0 FlashOverview
Given the scores \(A_i\) of \(N\) students, the problem asks us to count how many students have an absolute difference from the reference score \(S\) that is at most \(T\) (i.e., students satisfying \(|A_i - S| \leq T\)).
Analysis
What this problem requires is determining whether each student’s score \(A_i\) satisfies the following condition: $\(|A_i - S| \leq T\)$
Removing the absolute value sign, this is equivalent to the score falling within the following range: $\(S - T \leq A_i \leq S + T\)$
Checking the constraints, the number of students \(N\) is at most \(2 \times 10^5\). Since the check for each student can be done with a simple comparison, a straightforward approach of checking all students one by one (linear scan) is more than sufficient to run within the time limit.
Algorithm
- Read \(N, S, T\) and the list of scores \(A\) from the input.
- Initialize a variable
countto \(0\) to keep track of the number of passing students. - Loop through the scores of all \(N\) students one by one.
- For each score \(A_i\), compute \(|A_i - S|\) using the absolute value function (
abs()in Python). - If this value is at most \(T\), increment
countby \(1\).
- For each score \(A_i\), compute \(|A_i - S|\) using the absolute value function (
- Output the final value of
count.
Complexity
- Time Complexity: \(O(N)\)
- For \(N\) students, we perform one check per student, so the processing time is proportional to \(N\). Even when \(N = 2 \times 10^5\), modern computers can handle this in a few milliseconds to a few tens of milliseconds.
- Space Complexity: \(O(N)\)
- If all input scores are stored in a list, the memory usage is proportional to the number of scores.
Implementation Notes
Fast Input: Since \(N\) can be as large as \(2 \times 10^5\), calling
input()repeatedly in Python may lead to long execution times. By usingsys.stdin.read().split()to read all input at once, processing can be done more efficiently.Absolute Value Calculation: While you could use conditional branching (if statements) to check whether \(A_i - S\) is positive or negative, as per the mathematical definition, using the built-in
abs()function provided by the programming language allows for more concise code.Source Code
import sys
def solve():
# 入力を一括で読み込み、スペース区切りで分割する
input_data = sys.stdin.read().split()
if not input_data:
return
# N, S, T を取得
n = int(input_data[0])
s = int(input_data[1])
t = int(input_data[2])
# 生徒の点数リストを取得
a_list = input_data[3:]
count = 0
# 各生徒の点数について判定を行う
for i in range(n):
a_i = int(a_list[i])
# |A_i - S| <= T かどうかをチェック
if abs(a_i - s) <= t:
count += 1
# 結果を出力
print(count)
if __name__ == "__main__":
solve()
This editorial was generated by gemini-3-flash-preview.
投稿日時:
最終更新: