Problem Solving

[백준 BOJ - 1966 / 파이썬] 프린터 큐

Oh Seokjin 2022. 7. 30. 00:14

1966번: 프린터 큐 (acmicpc.net)

 

1966번: 프린터 큐

여러분도 알다시피 여러분의 프린터 기기는 여러분이 인쇄하고자 하는 문서를 인쇄 명령을 받은 ‘순서대로’, 즉 먼저 요청된 것을 먼저 인쇄한다. 여러 개의 문서가 쌓인다면 Queue 자료구조에

www.acmicpc.net

 

🔍 문제 분석

문서들의 우선순위가 주어진다. target index가 몇 번째로 출력되는지 계산하는 문제

 

❗ 해결 아이디어 

리스트 두 개를 함께 돌리는 것이 핵심.

리스트 하나로 처리하게 되면, 우선순위가 중복되는 경우를 생각하기 복잡해진다.

 

✔️ 최종 풀이

import sys

n = int(sys.stdin.readline())

for _ in range(n):
    doc_cnt, target = map(int,sys.stdin.readline().split())
    doc_list = list(enumerate(map(int, sys.stdin.readline().split())))
    priority_list = [doc_list[i][1] for i in range(doc_cnt)]
    answer = 0

    while True:
        if priority_list[0] == max(priority_list):
            answer += 1
            if doc_list[0][0] == target:
                print(answer)
                break
            else:                
                priority_list.pop(0)
                doc_list.pop(0)
        else:
            priority_list.append(priority_list.pop(0))
            doc_list.append(doc_list.pop(0))