tier 프린터 큐

풀이

맵이나 배열을 이용해서 중요도에 따른 문서의 개수를 저장하고, 문서 중 가장 높은 중요도를 기억합시다.

이제 문제에서 설명한 대로 큐를 조작합니다. 만약 큐의 가장 앞 문서가 가장 높은 중요도인 문서라면 출력하고, 아니라면 맨 뒤로 다시 넣습니다.

만약 가장 높은 중요도의 문서가 전부 인쇄된 경우 중요도를 1씩 낮춰보면서 남은 문서의 중요도 중 가장 높은 중요도를 찾습니다.

이를 문서가 번 인쇄될 때 까지 계속 반복하면 됩니다.

큐에서 가장 중요도가 높은 문서가 항상 맨 뒤에 있다고 해도 이면 해결 가능합니다.

코드

from collections import deque
 
 
def solve():
    N, M = map(int, input().split())
 
    count = [0] * 10
    priority = list(map(int, input().split()))
    max_priority = max(priority)
    for p in priority:
        count[p] += 1
 
    queue = deque(range(N))
    printed_count = 0
    while True:
        if priority[queue[0]] != max_priority:
            queue.append(queue.popleft())
            continue
 
        if queue[0] == M:
            print(printed_count + 1)
            return
 
        printed_count += 1
        queue.popleft()
 
        count[max_priority] -= 1
        while count[max_priority] == 0:
            max_priority -= 1
 
 
def main():
    T = int(input())
 
    for _ in range(T):
        solve()
 
 
main()