Official

A - テストの点数比較 / Comparing Test Scores Editorial by admin

Gemini 3.0 Flash

Overview

This problem requires answering \(Q\) questions (“Is student \(a\)’s score higher than student \(b\)’s score?”) with either Yes or No, given the test scores of \(N\) students.

Analysis

The steps required to solve this problem are as follows:

  1. Data Storage: We need to store each student’s score associated with their student number. By using an array (a list in Python), we can retrieve the score of student number \(i\) in \(O(1)\).
  2. Index Adjustment: In the problem statement, student numbers are given from \(1\) to \(N\) (1-indexed), but arrays in programs typically start from \(0\) (0-indexed). Therefore, to access the data of student number \(a\), we need to reference the a-1-th element of the array.
  3. Efficient Processing: Looking at the constraints, both \(N\) and \(Q\) can be as large as \(10^5\), so we need to answer each question quickly. Using an array allows us to answer each question in \(O(1)\), resulting in \(O(Q)\) overall, which is fast enough.
  4. Fast I/O: In Python, repeatedly using input() and print() for large amounts of I/O spanning \(10^5\) lines can cause the I/O itself to be slow, leading to Time Limit Exceeded (TLE). Using sys.stdin.read().split() and sys.stdout.write() enables faster processing.

Algorithm

  1. Read \(N\) and \(Q\) from the input.
  2. Store the \(N\) scores in a list H.
  3. For each question \((a, b)\), perform the following check:
    • If H[a-1] > H[b-1], then student \(a\)’s score is higher, so output Yes.
    • Otherwise (including the case where they are equal), output No.
  4. Output all answers together at once.

Complexity

  • Time Complexity: \(O(N + Q)\)
    • Reading the scores takes \(O(N)\), and processing each question takes \(O(1) \times Q\), so the overall complexity is \(O(N + Q)\).
  • Space Complexity: \(O(N + Q)\)
    • The list storing student scores uses \(O(N)\) memory, and temporarily holding the query answers uses \(O(Q)\) memory.

Implementation Notes

  • Comparison Operator: Since the condition is “strictly higher,” we use > (greater than) rather than >= (greater than or equal to).

  • Fast I/O:

    • Using sys.stdin.read().split() reads all input at once and creates a list split by whitespace and newlines.

    • By accumulating answers in a list results and outputting them all at once at the end with "\n".join(results), we reduce the number of output calls and improve performance.

      Source Code

import sys

def solve():
    # 標準入力からすべてのデータを取得し、空白で分割してリスト化する
    # 大量の入力を高速に処理するために sys.stdin.read().split() を使用
    input_data = sys.stdin.read().split()
    if not input_data:
        return
    
    # 生徒の数 N と 質問の数 Q を取得
    N = int(input_data[0])
    Q = int(input_data[1])
    
    # 各生徒の点数 H_i を取得して整数リストに変換
    # 出席番号 i は 1 から始まるため、リスト H のインデックス 0 が出席番号 1 に対応する
    H = list(map(int, input_data[2:2+N]))
    
    # 質問データを取得
    # 各質問は出席番号 a と b のペアで構成される
    queries = input_data[2+N:]
    
    results = []
    # 各質問に対してループを回す
    for i in range(0, 2 * Q, 2):
        # 出席番号 a と b を取得(1-indexed)
        a = int(queries[i])
        b = int(queries[i+1])
        
        # 生徒 a の点数 H[a-1] と生徒 b の点数 H[b-1] を比較
        # a の点数が b の点数より高い場合のみ "Yes"、それ以外は "No"
        if H[a-1] > H[b-1]:
            results.append("Yes")
        else:
            results.append("No")
    
    # すべての結果を改行区切りでまとめて出力
    sys.stdout.write("\n".join(results) + "\n")

if __name__ == "__main__":
    solve()

This editorial was generated by gemini-3-flash-preview.

posted:
last update: