Official

A - 最適な練習相手 / Optimal Practice Partner Editorial by admin

Qwen3-Coder-480B

Overview

Find the member with the highest skill value among those Takahashi can beat (if there are multiple such members, find the one with the smallest index).

Analysis

In this problem, given Takahashi’s skill value \(L\), we need to select the member \(i\) satisfying \(D_i \leq L\) who has the maximum \(D_i\), and among those with the maximum \(D_i\), the one with the smallest index.

A straightforward approach of checking everyone’s skill values and finding the maximum among those satisfying the condition is natural. However, if we sort or compare repeatedly each time, the time complexity could become too large to fit within the time limit (especially since \(N\) can be up to \(2 \times 10^5\)).

However, this problem can actually be solved with a simple linear scan without sorting. The key points are:

  • For each member, check whether their skill value \(D_i\) is less than or equal to Takahashi’s skill value \(L\).
  • Among those satisfying the condition, find the member with the maximum \(D_i\).
  • If multiple members share the same maximum value, select the one with the smallest index (i.e., the one that appears first).

Therefore, by scanning from the beginning in order, we naturally prioritize smaller indices while finding the member with the maximum skill value.

Algorithm

  1. Scan the skill value list \(D\) from the beginning.
  2. For each member \(i\) (0-indexed):
    • If \(D[i] \leq L\), check whether it is greater than the maximum skill value seen so far.
    • If it is greater, record that skill value and index.
  3. After the scan, the recorded index (+1 to convert back to 1-indexed) is the answer.
  4. If no update was ever made, output -1.

By managing the necessary information in a single loop, we can efficiently find the solution.

Complexity

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

Implementation Notes

  • Since indices start from 0, output i + 1 for the result.

  • Initializing max_d to -1 or some very small value makes the initial comparison straightforward.

  • To handle the case where Takahashi cannot beat anyone, we need to return -1 if no update was made by the end of the scan.

    Source Code

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

max_d = -1
ans = -1

for i in range(N):
    if D[i] <= L:
        if D[i] > max_d:
            max_d = D[i]
            ans = i + 1

print(ans)

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

posted:
last update: