
문서가 여러 개 있는 프린터에서,
프린터는 항상 가장 중요도가 높은 문서부터 인쇄하려고 한다.
인쇄 요청한 문서들이 언제 인쇄되는 지 순서를 출력
import sys
N = int(sys.stdin.readline())
docs = []
result = []
sum =0
for _ in range(N):
doc, order = map(int, sys.stdin.readline().split())
priorities = list(map(int, sys.stdin.readline().split()))
docs.append((doc,order,max(priorities)))
while True:
# 맨 앞 문서와 두 번째 문서만 비교 -> 전체 중 최대값을 봐야함
if docs[0][1] > docs[1][1]:
docs.insert((docs[0][0],docs[0][1],docs[0][2]))
docs.pop(0)
else:
break
result = [1]
for i in range(len(docs)-1):
result.append(docs[i][0]+1)
for _ in result:
print(_)
docs[0][1] > docs[1][1] : 전체 큐 중 최대 중요도와 비교해야하는 데 1번 2번 문서만 비교하고 있음max(priorities) 저장해두는 방식 : 문서 하나하나 비교해야 하므로 저장은 큰 의미 없음import sys
from collections import deque
N = int(sys.stdin.readline())
for _ in range(N):
doc, order = map(int, sys.stdin.readline().split()) # 문서 수, 문서위치
priorities = list(map(int, sys.stdin.readline().split())) # 각 문서의 우선순위
queue = deque((i, p) for i, p in enumerate(priorities)) # (문서번호, 중요도)
count = 0
while queue:
current = queue.popleft()
# 뒤에 더 높은 우선순위가 하나라도 있다면 다시 뒤로 보냄
if any(current[1] < q[1] for q in queue):
queue.append(current)
else:
count += 1 # 문서를 인쇄함
if current[0] == order: # 내가 찾던 문서면 출력
print(count)
break
popleft()와 append() 연산이 빠르고 직관적임any(current[1] < q[1] for q in queue)를 통해 더 높은 중요도 존재 insert()/pop(0) 제거 → 코드 안정성 및 효율 향상collections.deque를 활용하면 효율적으로 popleft()와 append()가 가능하다.any() 함수를 사용하면 “뒤에 더 높은 중요도가 있는지” 쉽게 검사할 수 있다.any(current_priority < other_priority for other in queue)