A - 最適な練習相手 / Optimal Practice Partner Editorial by admin
GPT 5.2 HighOverview
This problem asks you to find, among the members that Takahashi can beat (\(D_i \leq L\)), the one with the maximum skill value \(D_i\) (breaking ties by choosing the smallest index), and output that member’s number. If there is no one he can beat, output -1.
Analysis
The conditions for recommendation can be organized into two steps:
- Beatable opponents: Only members satisfying \(D_i \leq L\) are candidates.
- Optimal among candidates: Choose the member with the maximum \(D_i\) (if multiple members share the same maximum value, choose the one with the smallest index).
The key insight here is that we only need to find “the beatable opponent with the maximum \(D_i\)”, so a single pass through all members is sufficient. For each member, we check whether they can be beaten (\(D_i \leq L\)), and if so, compare against “the best so far (maximum \(D\))” and update accordingly.
When there is a tie (same \(D_i\)), we need to choose the smallest index. In our implementation, the update condition is d > best_val only, so ties do not trigger an update, and the first member found (= the one with the smallest index) is preserved. This naturally satisfies the tiebreaking condition.
Example:
- \(L=10\), \(D=[8,10,10,9]\)
The maximum among beatable opponents is \(10\). The first occurrence of \(10\) is member 2, so the answer is 2.
Algorithm
best_val = -1(the maximum skill value among beatable opponents seen so far)best_idx = -1(that member’s index)- For \(i=1..N\) in order, do the following:
- If \(D_i \leq L\) and \(D_i > best\_val\), then
update
best_val = D_i,best_idx = i
- If \(D_i \leq L\) and \(D_i > best\_val\), then
update
- Finally, output
best_idx(if it was never updated, it remains-1, so output-1)
Complexity
- Time complexity: \(O(N)\) (just a single pass through all members)
- Space complexity: \(O(1)\) (no additional large arrays are used; excluding the storage for the input array)
Implementation Notes
Not updating on ties (using
>as the condition) automatically satisfies “if multiple members share the maximum value, choose the smallest index”.If
best_idxremains-1until the end, there are no beatable opponents, so output-1.Since the constraints can be large, in Python it is safer to read input quickly using
sys.stdin.buffer.read().Source Code
import sys
def main():
data = list(map(int, sys.stdin.buffer.read().split()))
if not data:
return
N, L = data[0], data[1]
D = data[2:2+N]
best_val = -1
best_idx = -1
for i, d in enumerate(D, 1):
if d <= L and d > best_val:
best_val = d
best_idx = i
print(best_idx if best_idx != -1 else -1)
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.2-high.
posted:
last update: