본문 바로가기

BOJ 알고리즘 (패캠)/자료구조, 구현

1966번: 프린터큐 (구현, 큐) - Fastcampus

www.acmicpc.net/problem/1966

 

1966번: 프린터 큐

첫 줄에 test case의 수가 주어진다. 각 test case에 대해서 문서의 수 N(100이하)와 몇 번째로 인쇄되었는지 궁금한 문서가 현재 Queue의 어떤 위치에 있는지를 알려주는 M(0이상 N미만)이 주어진다. 다음

www.acmicpc.net

from collections import deque


def run(N, M, a):
    qu = deque([(p, i) for i, p in enumerate(a)])  # 큐

    cnt = 0
    while qu:
        cp, ci = qu.popleft()
        if all(cp >= p for p, i in qu):  # 중요도가 최대값이면 출력
            cnt += 1
            if ci == M:
                return cnt
        else:
            qu.append((cp, ci))


T = int(input())
for _ in range(T):
    N, M = map(int, input().split())
    a = list(map(int, input().split()))
    print(run(N, M, a))