B - 料理コンテスト / Cooking Contest 解説 by admin
DeepSeek V3Overview
This problem asks you to find the participant number with the highest final score, where the final score is the sum of scores given by Takahashi and Aoki for each participant. It is guaranteed that there is exactly one participant with the highest final score.
Analysis
This is a straightforward problem where you calculate the total value (final score) by summing the scores from the two judges for each participant, then find the participant with the maximum value. The key point is that it is guaranteed that there are not multiple participants with the maximum final score, so there is exactly one maximum value.
A naive approach would be to compute the total score for each participant in order, updating the maximum value and the winner whenever a value larger than the current maximum is found. This method runs sufficiently fast even under the problem’s constraints (N ≤ 200,000).
Algorithm
- Read the number of participants N
- Set the maximum score to -1 (initial value) and the winner’s number to -1 (initial value)
- For each participant (i = 1 to N):
- Read Takahashi’s score A_i and Aoki’s score B_i
- Compute the total score: total = A_i + B_i
- If total is greater than the current maximum score, update the maximum score to total and the winner’s number to i
- Output the final winner’s number
Complexity
- Time complexity: \(O(N)\)
- Because we perform a single loop over the N participants
- Space complexity: \(O(N)\)
- Memory usage for storing the input data
Implementation Notes
By setting the initial value of the maximum score to -1, it is kept smaller than any participant’s total score (the minimum possible total is 2)
Since participant numbers start from 1, the loop index can be used directly as the winner’s number
Reading the input data all at once enables efficient processing
Source Code
import sys
def main():
data = sys.stdin.read().splitlines()
n = int(data[0])
max_score = -1
winner = -1
for i in range(1, n + 1):
a, b = map(int, data[i].split())
total = a + b
if total > max_score:
max_score = total
winner = i
print(winner)
if __name__ == "__main__":
main()
This editorial was generated by deepseekv3.
投稿日時:
最終更新: