Official

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

Qwen3-Coder-480B

Overview

Given exam score data for students, find the student number of the passing student who has the highest score and the smallest student number, within a specified score range.

Analysis

In this problem, each student’s score is given, and students whose scores fall within a range \([L, R]\) are considered “passing.” Among the passing students, we need to find the student with the “highest score” and output their student number. If there are multiple students with the same highest score, we need to choose the one with the smallest student number. If no one passes, we output -1.

A straightforward approach would be to look at all students, list the passing ones, and select the one with the maximum score and smallest student number. However, since the number of students \(N\) can be up to \(2 \times 10^5\), we need to process this efficiently. Fortunately, the score range is very small, from \(0\) to \(100\), so approaches like managing students by score are possible, but even a simple linear scan is sufficient.

In this solution, we only need to look at all students once, continuously updating the best candidate among those who satisfy the condition. Specifically, if a passing student’s score is greater than the current maximum score, we update it; if the score is the same, we choose the one with the smaller student number.

Algorithm

  1. Read the number of students \(N\), the passing range \([L, R]\), and each student’s score \(P_i\) from input.
  2. Initialize the maximum score max_score to \(-1\) and the result student number result_index to \(-1\).
  3. For each student, do the following:
    • If score \(P_i\) is at least \(L\) and at most \(R\), the student passes.
    • In this case, update under the following conditions:
      • If the current score is greater than max_score → update
      • If the score is the same and the student number is smaller than the current result_index → update
  4. After the full scan, output result_index (it remains \(-1\) if no one passed).

Examples

Example input:

5 60 90
50 80 70 90 80
  • Passing students are student number 2 (80 points), 3 (70 points), 4 (90 points), 5 (80 points)
  • The highest score is 90 points, corresponding to student number 4 → Output: 4

Another example:

3 50 60
70 80 90
  • No passing students → Output: -1

Complexity

  • Time complexity: \(O(N)\)
  • Space complexity: \(O(N)\) (for storing the score list)

Implementation Notes

  • Note that student numbers are 1-indexed (starting from 1), so use i + 1 since array indices start from 0

  • Correctly implement the condition to choose the smaller student number when scores are equal

  • Don’t forget to return -1 when there are no passing students

    Source Code

N, L, R = map(int, input().split())
P = list(map(int, input().split()))

max_score = -1
result_index = -1

for i in range(N):
    if L <= P[i] <= R:
        if P[i] > max_score or (P[i] == max_score and (result_index == -1 or i + 1 < result_index)):
            max_score = P[i]
            result_index = i + 1

print(result_index)

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

posted:
last update: