B - 料理コンテスト / Cooking Contest 解説 by admin
Qwen3-Coder-480BOverview
Find the participant number with the highest total score given by Takahashi and Aoki combined.
Approach
For each participant \(i\), Takahashi’s score \(A_i\) and Aoki’s score \(B_i\) are given, and the final score is calculated as \(A_i + B_i\). We simply need to identify the participant with the maximum value among these.
A naive approach would be to store all participants’ scores in an array and then search for the maximum value. However, since the number of participants \(N\) can be up to \(2 \times 10^5\), using extra memory or nested loops could cause TLE. Instead, we can update the maximum value on the fly as we read each participant’s score, eliminating the need to maintain an array and solving the problem efficiently.
In this approach, each time we read a score, we compare it with the current maximum score. If the new score is higher, we record it as the new maximum score and simultaneously record that participant’s number.
Algorithm
- Initialize the maximum score
max_scoreto a very small value (e.g., \(-1\)). - Similarly initialize the winner’s number
winner. - For each participant, do the following:
- Read Takahashi’s score \(A_i\) and Aoki’s score \(B_i\) from input.
- Calculate the total score \(A_i + B_i\).
- If this score is greater than the current
max_score, updatemax_scoreandwinner.
- After processing all participants, output the value of
winner.
Complexity
- Time complexity: \(O(N)\)
(Since we only perform constant-time operations for each participant, the problem can be solved in linear time) - Space complexity: \(O(1)\)
(Since we only use a few variables without using arrays or similar data structures)
Implementation Notes
The input is 1-indexed (i.e., participant numbers start from 1), so the for loop should iterate over
range(1, N+1).Make sure to initialize
max_scoreto a sufficiently small value (since all scores are positive, initializing to a negative number is safe).By managing the maximum value incrementally as we read each participant’s score, we can minimize memory usage.
Source Code
N = int(input())
max_score = -1
winner = -1
for i in range(1, N + 1):
A, B = map(int, input().split())
score = A + B
if score > max_score:
max_score = score
winner = i
print(winner)
This editorial was generated by qwen3-coder-480b.
投稿日時:
最終更新: