이숭간 공부기록

백준 1966 파이썬 _ 프린터 큐 본문

알고리즘/백준

백준 1966 파이썬 _ 프린터 큐

이숭간 2021. 2. 12. 22:00
728x90

www.acmicpc.net/problem/1966

 

1966번: 프린터 큐

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

www.acmicpc.net

문제유형 : 큐, 구현, 시뮬레이션

문제핵심 : 기본적이 큐 형태에서, 조건을 확인하고 조건에맞게 구현해주면됨

 

이전에 프로그래머스에서 풀었던 문젠데 그떄는 정리를 안했어서 다시 내가풀었던 방식을 안보고 새로풀어보았다.

그때랑 아이디어?는 비슷한데 조금 다른 방식으로 풀었군

 

풀이1 (젤 최근풀이)

from collections import deque

def solution(priority, location):
    queue1 = deque()
    queue2 = deque()
    printed = []

    for i,v in enumerate(priority):
        queue1.append(i)
        queue2.append(v)

    while queue1:  # 큐가 빌때까지
        curr_val = queue2.popleft()
        curr_index = queue1.popleft()
        if len(queue2) == 0:  # 큐에 하나밖에 안남아서 비어버리면
            printed.append(curr_index)
        elif max(queue2) <= curr_val:  # 지금뽑은게 제일 중요도높다는것 -> 프린트해버렷
            printed.append(curr_index)
        else:  # 더 중요한게 뒤에 남아있당.
            queue2.append(curr_val)
            queue1.append(curr_index)

    print(printed.index(location)+1) #location인덱스는 몇번쨰로 출력되었는가? --> printed배열의 값이 location인 인덱스


import sys
input = sys.stdin.readline
test_case = int(input())
result = []

for _ in range(test_case):
    n,location = map(int, input().split())
    priority = list(map(int, input().split()))
    result.append(solution(priority, location))

 

풀이2 (예전풀이)

def solution(prior, location):
    sort = sorted(prior, reverse=True)

    answer = 0
    index = []

    for i in range(len(prior)):
        value = (i, prior[i])
        index.append(value)

    for i in range(len(index)**2):
        max = sort[0]

        if max == index[0][1]: # 맨앞에가 가장 큰값일때
            curr = index[0]
            index.pop(0)
            sort.pop(0)
            answer += 1
            if curr[0] == location:
                return answer
            else:
                continue
        else:
            curr = index[0]
            index.pop(0)
            index.append(curr)


import sys
input = sys.stdin.readline
test_case = int(input())
result = []

for _ in range(test_case):
    n,location = map(int, input().split())
    priority = list(map(int, input().split()))
    result.append(solution(priority, location))

for i in result:
    print(i)