Official

B - クラス委員長の選出 / Election of the Class President Editorial by admin

Gemini 3.0 Flash

Overview

We tentatively designate the student with attendance number 1 as the committee chair, then search among the remaining students (attendance number 2 onward) to see if anyone has a higher leadership score than the current chair. If multiple students satisfy the condition, we select the one with the highest score, and among those with the same score, the one with the smallest attendance number.

Analysis

To solve this problem, we need to check the scores of all students in order.

  1. Comparison baseline: First, we store the score \(A_1\) of the student with attendance number 1 as our baseline.
  2. Narrowing down candidates: We examine students with attendance numbers \(2, 3, \dots, N\) one by one, considering only those whose score is greater than \(A_1\) as candidates.
  3. Updating the best candidate: Among the candidates, we look for the student with the highest score. The key point here is what to do when multiple students have the same score.
    • The rules state “if scores are the same, choose the one with the smaller attendance number.”
    • If we examine students in order of increasing attendance number (from \(2\) to \(N\)), and only save a new student’s information when they strictly exceed the previously recorded maximum score, then when scores are tied, the first student found (with the smaller attendance number) is naturally retained.

Since \(N\) can be as large as \(2 \times 10^5\), we need an \(O(N)\) algorithm that finds the answer in a single pass (one loop) without using nested loops.

Algorithm

We process as follows:

  1. Store the score \(A_1\) of attendance number 1 in a variable (e.g., a1).
  2. Initialize a variable max_found_score to hold “the maximum score found so far” to \(-1\), and a variable best_id to hold “the attendance number of that student” to \(-1\).
  3. For attendance numbers \(i = 2, 3, \dots, N\), repeat the following:
    • If \(A_i > A_1\) and \(A_i > max\_found\_score\):
      • Update max_found_score to \(A_i\).
      • Update best_id to \(i\).
  4. Output the final best_id. If it was never updated, the initial value of \(-1\) is output as is.

Complexity

  • Time complexity: \(O(N)\) We loop through the scores only once for \(N\) students, so the computation finishes in time proportional to the number of students.
  • Space complexity: \(O(N)\) Since we store all input scores in a list, memory consumption is proportional to \(N\).

Implementation Notes

  • Fast I/O: When handling input of around \(N=2 \times 10^5\) in Python, repeatedly calling input() can be slow. By using sys.stdin.read().split(), we can read all input at once and process it efficiently.

  • Inequality in the update condition: By using the strict inequality score > max_found_score (“strictly greater than”), we ensure that best_id is not updated when scores are equal. This easily satisfies the condition “if scores are the same, choose the student with the smallest attendance number.”

    Source Code

import sys

def solve():
    # 標準入力からすべてのデータを読み込み、空白で分割してリストにします。
    # 大量の入力を高速に処理するために sys.stdin.read().split() を使用します。
    data = sys.stdin.read().split()
    if not data:
        return
    
    # 生徒の人数 N を取得します。
    n = int(data[0])
    
    # 出席番号 1 の生徒(現在の委員長)のリーダーシップ点数 A1 を取得します。
    a1 = int(data[1])
    
    # 新しい委員長候補の出席番号と、その中での最大スコアを初期化します。
    best_id = -1
    max_found_score = -1
    
    # 出席番号 2 以降の生徒を順番に確認します。
    # ループの i は 1 から N-1 まで動き、data[i+1] が出席番号 i+1 の生徒の点数に対応します。
    for i in range(1, n):
        score = int(data[i + 1])
        
        # ルール1: 現在の委員長 A1 よりもリーダーシップ点数が真に大きいか確認します。
        if score > a1:
            # その中で最もリーダーシップ点数が高い生徒を選びます。
            # 既に記録されている最大値よりも大きい場合のみ更新することで、
            # 同じ点数の場合は「出席番号が最も小さい生徒」が保持されます。
            if score > max_found_score:
                max_found_score = score
                best_id = i + 1
                
    # 委員長が変更された場合はその出席番号を、変更がない場合は -1 を出力します。
    print(best_id)

if __name__ == "__main__":
    solve()

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

posted:
last update: