Official

B - 試験の合格者 / Exam Passers Editorial by admin Editorial for printing

GPT 5.2 High

Overview

This problem asks you to find the student ID number of the student with the highest score among those whose scores fall within the passing range \([L, R]\) (if there is a tie, choose the smallest student ID number). If there are no passing students, output -1.

Analysis

  • We want to find the “maximum score among passing students” and “the smallest student ID number with that score.”
  • Each student’s score is in the range \(0 \sim 100\), which is small, but \(N\) can be up to \(2\times 10^5\), so an \(O(N)\) approach that examines each student once is sufficiently fast.
  • For the “find the person with the maximum score” problem, the standard technique is to scan through the data while updating the current best (maximum).
  • For tie-breaking (when multiple people share the maximum score, choose the smallest student ID number): if we don’t update on ties, the first person found (= the one with the smallest student ID number) remains.
    • Example: If the scores within the passing range are [80(#1), 90(#2), 90(#3)],
      the maximum becomes 90 at student #2, and since student #3 has the same score, if we don’t update, the answer stays as #2 (which is correct as the smallest ID number).
  • To handle the case where there are no passing students at all, it is simple to use an initial value of -1 to represent “not yet found.”

Algorithm

  1. Initialize best_score = -1, best_idx = -1 (state where no passing student has been found yet).
  2. Iterate through students in order \(i = 1 \ldots N\).
  3. Only consider a student as a candidate when their score score satisfies the passing range \(L \le score \le R\).
  4. If that score is greater than best_score, update best_score = score, best_idx = i.
    • When score == best_score, we do not update, so the smaller student ID number is preserved.
  5. Finally, output best_idx (it remains -1 if no passing student was found).

Complexity

  • Time complexity: \(O(N)\) (we only check each student once)
  • Space complexity: \(O(1)\) (only a constant number of variables for the best value and index)

Implementation Notes

  • Since student ID numbers are \(1\)-indexed, using something like enumerate(P, 1) to count from \(1\) helps prevent mistakes.

  • To prioritize the smallest ID number in case of ties, the update condition should be score > best_score only (do not use >=).

  • To handle the case where there are no passing students, initialize best_idx to -1 so it can be output directly.

    Source Code

import sys

def main():
    data = list(map(int, sys.stdin.buffer.read().split()))
    if not data:
        return
    N, L, R = data[0], data[1], data[2]
    P = data[3:3+N]

    best_score = -1
    best_idx = -1

    for i, score in enumerate(P, 1):
        if L <= score <= R:
            if score > best_score:
                best_score = score
                best_idx = i

    print(best_idx)

if __name__ == "__main__":
    main()

This editorial was generated by gpt-5.2-high.

posted:
last update: