公式

B - 料理コンテスト / Cooking Contest 解説 by admin

Claude 4.6 Opus (Thinking)

Overview

For each of the \(N\) participants, compute the sum of the scores given by two judges (the final score), and output the number of the participant with the highest final score.

Analysis

This problem is very straightforward: calculate \(A_i + B_i\) for each participant and find the participant number that has the maximum value.

For example, consider the case where \(N = 3\) and the following input is given:

Participant \(A_i\) \(B_i\) Final Score \(A_i + B_i\)
1 50 60 110
2 80 90 170
3 70 80 150

The participant with the highest final score is participant 2 (score 170), so the answer is 2.

In this problem, \(N\) can be up to \(2 \times 10^5\), but since we only perform one addition and one comparison for each participant, no special algorithm is needed. A simple loop that iterates through all participants once is sufficient.

Algorithm

  1. Initialize the maximum score best_score to \(-1\) (a sufficiently small value) and also initialize the corresponding participant number best_idx.
  2. For participants \(i = 1, 2, \dots, N\), read \(A_i\) and \(B_i\) in order and compute \(A_i + B_i\).
  3. If \(A_i + B_i\) is greater than the current best_score, update best_score and best_idx.
  4. After processing all participants, output best_idx.

This is essentially the basic operation of “finding the maximum value.” You could also store all the data in an array and then search for the maximum, but comparing incrementally while reading input is more memory-efficient.

Complexity

  • Time complexity: \(O(N)\) — since each participant is processed exactly once
  • Space complexity: \(O(1)\) — since only the maximum score and its corresponding number are stored (input data is not saved in an array)

Implementation Notes

  • Note that participant numbers are 1-indexed (starting from 1). When looping with Python’s range, using range(1, N + 1) allows you to handle participant numbers directly.

  • The initial value of best_score can be any value smaller than the minimum possible score (\(1 + 1 = 2\)). In the code, \(-1\) is used.

  • The problem guarantees that “there is exactly one participant with the highest final score,” so there is no need to handle ties.

    Source Code

N = int(input())
best_score = -1
best_idx = -1
for i in range(1, N + 1):
    a, b = map(int, input().split())
    if a + b > best_score:
        best_score = a + b
        best_idx = i
print(best_idx)

This editorial was generated by claude4.6opus-thinking.

投稿日時:
最終更新: