백준 알고리즘 1966. 프린터 큐

2024. 3. 12. 20:35코딩 테스트/백준

https://www.acmicpc.net/problem/1966

 

1966번: 프린터 큐

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

www.acmicpc.net

 

전체 코드

import collections


def solutions(nums, m):
    prioritys = sorted(nums)
    print_queue = collections.deque()
    for i, n in enumerate(nums):
        # (우선순위, 인덱스)
        print_queue.append((n, i))

    pop_count = 0
    while print_queue:
        d = print_queue.popleft()
        if d[0] == prioritys[-1]:
            prioritys.pop()
            pop_count += 1
            if d[1] == m:
                break
        elif d[0] < prioritys[-1]:
            print_queue.append(d)

    return pop_count


t = int(input())
for _ in range(t):
    n, m = map(int, input().split())
    nums = list(map(int, input().split()))
    print(solutions(nums, m))