B - プログラミングコンテストの順位表 / Scoreboard of a Programming Contest 解説 by admin 印刷ページ
Qwen3-Coder-480BOverview
This is a problem where you determine rankings based on each participant’s score and rating according to specified rules, then output the participant numbers from highest to lowest rank.
Analysis
In this problem, each participant has the following three pieces of information:
- Score earned \(B_i\)
- Rating \(A_i\)
- Participant number \(i\)
The ranking is determined as follows:
- Higher score first (descending order)
- If scores are the same, higher rating first (descending order)
- If those are also the same, smaller participant number first (ascending order)
Therefore, we can determine the rankings by combining this information into tuples and sorting them.
A naive approach of finding the maximum each time would result in \(O(N^2)\) in the worst case, which would TLE given the constraint \(N \leq 2 \times 10^5\). Instead, we consider sorting everything at once.
In Python, the sort() function allows specifying a comparison key, so it is efficient to set these three conditions appropriately and sort.
Specifically, we sort tuples of (B_i, A_i, i) as follows:
- First key: \(B_i\) (descending →
-B_i) - Second key: \(A_i\) (descending →
-A_i) - Third key: \(i\) (ascending)
By setting the keys this way, we can sort according to the ranking rules of the problem.
Algorithm
- For each participant, create a tuple
(B_i, A_i, i)and store it in a list. - Sort this list using the following keys:
-B_i(descending order of score)-A_i(descending order of rating)i(ascending order of number)
- Output the participant numbers in the sorted order.
Complexity
- Time complexity: \(O(N \log N)\) (due to sorting)
- Space complexity: \(O(N)\) (for storing participant information)
Implementation Notes
sys.stdin.readis used for fast input reading.By setting the sort key as
(-B, -A, i), multiple conditions can be handled at once.When extracting tuple elements, using underscores
_to explicitly denote ignored variables improves readability.Source Code
import sys
input = sys.stdin.read
data = input().split()
N = int(data[0])
participants = []
index = 1
for i in range(N):
A = int(data[index])
B = int(data[index+1])
participants.append((B, A, i+1))
index += 2
participants.sort(key=lambda x: (-x[0], -x[1], x[2]))
for _, _, num in participants:
print(num)
This editorial was generated by qwen3-coder-480b.
投稿日時:
最終更新: