[白俊7662号]双優先級行列
https://www.acmicpc.net/problem/7662
1.コード
1.コード import heapq
import sys
from collections import defaultdict
ans = []
k = int(input())
for _ in range(k):
n = int(input())
d = defaultdict()
min_h = []
max_h = []
idx = 0
for _ in range(n):
c, i = sys.stdin.readline().rstrip().split()
i = int(i)
if c == 'I':
heapq.heappush(min_h, (i, idx))
heapq.heappush(max_h, (-i, idx))
d[idx] = [i, True] # True: 아직 삭제되지 않은 상태
idx += 1
else:
if i == 1:
# 뽑은 값의 상태가 False: 이미 다른 힙에서 제거된 원소
# 뽑은 값이 True 를 찾을 때까지 계속 값을 찾는다. while - if 순서 주의
while max_h and not d[max_h[0][1]][1]:
heapq.heappop(max_h)
# while 문을 거치고 나면 이 힙에서 뽑는 값은 True 상태 보장
if max_h:
d[heapq.heappop(max_h)[1]][1] = False
elif i == -1:
while min_h and not d[min_h[0][1]][1]:
heapq.heappop(min_h)
if min_h:
d[heapq.heappop(min_h)[1]][1] = False
result = []
for i in range(idx):
if d[i][1]:
result.append(d[i][0])
if result:
ans.append((max(result), min(result)))
else:
ans.append('EMPTY')
for i in ans:
if i == 'EMPTY':
print('EMPTY')
else:
print(i[0], i[1])
2.後期
入力値をhipに入れる際にハッシュ値としての変数idxを用いた.同じ数字がhipであってもidx値が異なるため、異なる値として認識できる.d[idx]
:
あるhipから抽出された最大値または最小値が別のhipから削除された要素である場合、d[idx]値はfalseとなります.while - if
:
抽出する要素のidxがTrueになるまでwhile文を使用します.pop()です.最初に抽出された要素のidxがtrueの場合、while文は無視され、if文を直接スキップすればよい.pop()の場合はwhile文を使用することが多く,while-if順序の使用はコードをより簡潔にする.
Reference
この問題について([白俊7662号]双優先級行列), 我々は、より多くの情報をここで見つけました
https://velog.io/@legowww/백준-7662번-이중-우선순위-큐
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
import heapq
import sys
from collections import defaultdict
ans = []
k = int(input())
for _ in range(k):
n = int(input())
d = defaultdict()
min_h = []
max_h = []
idx = 0
for _ in range(n):
c, i = sys.stdin.readline().rstrip().split()
i = int(i)
if c == 'I':
heapq.heappush(min_h, (i, idx))
heapq.heappush(max_h, (-i, idx))
d[idx] = [i, True] # True: 아직 삭제되지 않은 상태
idx += 1
else:
if i == 1:
# 뽑은 값의 상태가 False: 이미 다른 힙에서 제거된 원소
# 뽑은 값이 True 를 찾을 때까지 계속 값을 찾는다. while - if 순서 주의
while max_h and not d[max_h[0][1]][1]:
heapq.heappop(max_h)
# while 문을 거치고 나면 이 힙에서 뽑는 값은 True 상태 보장
if max_h:
d[heapq.heappop(max_h)[1]][1] = False
elif i == -1:
while min_h and not d[min_h[0][1]][1]:
heapq.heappop(min_h)
if min_h:
d[heapq.heappop(min_h)[1]][1] = False
result = []
for i in range(idx):
if d[i][1]:
result.append(d[i][0])
if result:
ans.append((max(result), min(result)))
else:
ans.append('EMPTY')
for i in ans:
if i == 'EMPTY':
print('EMPTY')
else:
print(i[0], i[1])
入力値をhipに入れる際にハッシュ値としての変数idxを用いた.同じ数字がhipであってもidx値が異なるため、異なる値として認識できる.
d[idx]
:あるhipから抽出された最大値または最小値が別のhipから削除された要素である場合、d[idx]値はfalseとなります.
while - if
:抽出する要素のidxがTrueになるまでwhile文を使用します.pop()です.最初に抽出された要素のidxがtrueの場合、while文は無視され、if文を直接スキップすればよい.pop()の場合はwhile文を使用することが多く,while-if順序の使用はコードをより簡潔にする.
Reference
この問題について([白俊7662号]双優先級行列), 我々は、より多くの情報をここで見つけました https://velog.io/@legowww/백준-7662번-이중-우선순위-큐テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol