本题最大的难点不是题目本身
而是如何处理牛客网的多组流数据输入输出
from heapq import *
while True:
try:
n = int(input())
direction = int(input())
heap = []
score_dict = {}
for _ in range(n):
l = input().split(' ')
this_score = int(l[1])
this_name = l[0]
if this_score not in score_dict:
score_dict[this_score] = [this_name]
else:
score_dict[this_score].append(this_name)
if direction == 1:
heappush(heap,this_score)
elif direction == 0:
heappush(heap,-1*this_score)
for _ in range(n):
if direction == 1:
this_score = heappop(heap)
else:
this_score = heappop(heap) * -1
this_person = score_dict[this_score][0]
score_dict[this_score].pop(0)
print(this_person + ' ' + str(this_score))
except:
break