A - テストの点数比較 / Comparing Test Scores 解説 by admin
GPT 5.2 HighOverview
Given each student’s score \(S_i\), for each of \(Q\) queries, determine whether “\(S_{a_i} > S_{b_i}\)” and output Yes/No.
Analysis
The key observation is that each query simply “compares the scores of two students” — there are no score updates or complex aggregations. Therefore, by storing the score array \(S\) in advance, each query can be answered by directly comparing \(S_{a_i}\) and \(S_{b_i}\).
Even the naive approach of “comparing each time” is all that needs to be done, and this itself is optimal (answering \(Q\) queries requires at least \(Q\) outputs).
However, since \(N, Q \le 10^5\), using slow I/O such as calling input() \(Q\) times in Python can easily result in TLE. Therefore, it is safe to use fast I/O (reading all input at once and outputting all at once).
Also, the problem statement explicitly states that cases where \(a_i = b_i\) may occur. In such cases, \(S_{a_i} > S_{b_i}\) is always false (since the values are the same), so the answer is No. No special handling is needed in the code — the comparison operator > handles this correctly as-is.
Examples:
- When \(S=[70,80,80]\), for \((a,b)=(2,3)\), \(80>80\) does not hold, so No
- For \((a,b)=(2,1)\), \(80>70\), so Yes
Algorithm
- Read \(N, Q\).
- Store the score sequence \(S_1,\dots,S_N\) as an array \(S\).
- For each query \((a_i,b_i)\):
- Since the input is 1-indexed, convert to 0-indexed: \(a=a_i-1\), \(b=b_i-1\)
- If \(S[a] > S[b]\), append
Yesto the output array; otherwise appendNo
- Finally, join the output array with newlines and print it all at once.
Complexity
- Time complexity: \(O(N+Q)\) (\(N\) for reading scores, \(Q\) for processing each query)
- Space complexity: \(O(N+Q)\) (\(N\) for the score array, up to \(Q\) for storing the output)
Implementation Notes
Fast I/O: Reading all input at once with
sys.stdin.buffer.read().split()and outputting all at once with"\n".join(...)ensures stable and fast performance.Watch the indices: Student numbers in the input are 1-indexed, so always subtract
-1before accessing the array.Strict inequality: The problem asks for “strictly higher,” so use
>instead of>=.Source Code
import sys
def main():
data = sys.stdin.buffer.read().split()
it = iter(data)
N = int(next(it))
Q = int(next(it))
H = [int(next(it)) for _ in range(N)]
out_lines = []
for _ in range(Q):
a = int(next(it)) - 1
b = int(next(it)) - 1
out_lines.append("Yes" if H[a] > H[b] else "No")
sys.stdout.write("\n".join(out_lines))
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.2-high.
投稿日時:
最終更新: