Official

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

Qwen3-Coder-480B

Overview

Student \(1\) is set as the initial committee leader, and the task is to select an appropriate new leader from among the students whose leadership scores are strictly higher.

Analysis

In this problem, among the students with scores higher than the current leader (student \(1\)), we need to select the student with the “highest score” and, in case of ties, the “smallest student number.”

A straightforward approach is to scan through all students to find one that satisfies the conditions, which runs in \(O(N)\) at worst and is sufficiently fast. However, care must be taken not to overlook the condition “if scores are equal, choose the student with the smaller student number” during the maximum value update process.

In this problem, by examining students other than student \(1\) in order, we can find the answer in a single pass.

For example, given the input:

5
10 20 30 20 10

The initial leader’s score is \(10\). Students \(2, 3, 4\) satisfy the condition, but the one with the highest score \(30\) is student \(3\), which is the answer.

Algorithm

  1. Let current_leader_score be the leadership score of student \(1\).
  2. Initialize variables max_score and new_leader to record the maximum score and the student number of that student (initialize each to \(-1\) or similar).
  3. Iterate through students \(2\) to \(N\) in order:
    • If the current student’s score is greater than current_leader_score:
      • Furthermore, if that score is greater than the previously recorded max_score, update max_score and new_leader.
  4. Finally, output new_leader (if it was never updated, it remains \(-1\)).

Complexity

  • Time complexity: \(O(N)\)
  • Space complexity: \(O(1)\) (excluding the input array)

Implementation Notes

  • Note that student numbers are \(1\)-indexed (since array indices start from \(0\), output i + 1).

  • There is no need to explicitly handle “if scores are equal, choose the student with the smaller student number” because by scanning from the front in order, the first one found naturally has the smallest student number.

  • The condition A[i] > current_leader_score checks for “strictly greater,” so an equality check is not needed.

    Source Code

N = int(input())
A = list(map(int, input().split()))

current_leader_score = A[0]
max_score = -1
new_leader = -1

for i in range(1, N):
    if A[i] > current_leader_score:
        if A[i] > max_score:
            max_score = A[i]
            new_leader = i + 1

print(new_leader)

This editorial was generated by qwen3-coder-480b.

posted:
last update: