A - ロボットバトル大会 / Robot Battle Tournament 解説 by admin
GPT 5.2 HighOverview
Since this is a tournament where the robot with higher combat power always wins, the ultimate champion is “the robot with the maximum combat power.” Therefore, we just need to find the robot number that has the maximum value of \(A_i\).
Analysis
In this tournament, the robot with higher combat power always wins every match (and moreover, all robots have distinct combat powers).
Then, for robot \(M\) with the highest overall combat power:
- It will always win no matter which opponent it faces
- Regardless of the tournament bracket (ordering or matchups), it will always win the championship in the end
This tells us that “there is no need to actually simulate the tournament bracket — we just need to find the maximum value.”
For example, if \(A=[5,1,4,3]\), in the first round (5 vs 1)→5, (4 vs 3)→4, and in the final (5 vs 4)→5, so the champion has combat power 5, which is the maximum.
Even if the ordering is different, the maximum value 5 cannot lose to anyone, so it always wins the championship.
You could naively simulate by creating a winner array for each round and still get the correct answer, but it is essentially the same as “finding the maximum value,” and the implementation becomes somewhat verbose. Here we find the maximum value (and its position) in the shortest way.
Algorithm
- Read input \(A_1, A_2, \ldots, A_N\)
- Maintain the current maximum combat power
best_valand its robot numberbest_idx(\(1\)-indexed) - Scan from left to right, updating whenever a larger combat power is found
- Output
best_idxat the end
This is the process of finding “the position of the maximum value in an array (argmax).”
Complexity
- Time complexity: \(O(N)\) (just a single pass)
- Space complexity: \(O(1)\) (when only maintaining the maximum value and its position. ※In the code, the input array is stored so it is effectively \(O(N)\), but as an algorithm it can be done in \(O(1)\))
Implementation Notes
Robot numbers are \(1\)-indexed, so note that for array index \(i\), the robot number is \(i+1\).
Since \(N \le 2^{20}\) is fairly large, in Python using
sys.stdin.buffer.read()allows for faster input.Source Code
import sys
def main():
data = sys.stdin.buffer.read().split()
if not data:
return
n = int(data[0])
a = list(map(int, data[1:1+n]))
best_idx = 1
best_val = a[0]
for i in range(1, n):
if a[i] > best_val:
best_val = a[i]
best_idx = i + 1
print(best_idx)
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.2-high.
投稿日時:
最終更新: