백준_1966 (프린터 큐_큐)

RostoryT·2022년 6월 22일
0

Stack Queue

목록 보기
13/17

메모

  1. 현재 Queue의 가장 앞에 있는 문서의 ‘중요도’를 확인한다.
  2. 나머지 문서들 중 현재 문서보다 중요도가 높은 문서가 하나라도 있다면, 이 문서를 인쇄하지 않고 Queue의 가장 뒤에 재배치 한다. 그렇지 않다면 바로 인쇄를 한다.

정답 코드

  • 자꾸 답이 안나와서 고생했는데
  • max()가 맞았음!
  • 그리고 맨앞 값이 max보다 크거나 or max인 경우에만 (current[0] >= max(que)[0])
    - 그중에서 내가 찾는 값(플래그 -1)이면 출력횟수 +1해서 print 후 끝
    • 아니면 stack안에서 제일 큰 값이니까 popleft() 하고 출력횟수 +1
'''내가 짠'''
import sys
from collections import deque

k = int(sys.stdin.readline())
for _ in range(k):
    n, m = map(int, sys.stdin.readline().split())
    arr = list(map(int, sys.stdin.readline().split()))

    que = deque([[i,0] for i in arr])
    que[m][1] = -1                              # -1로 주는게 

    cnt = 0

    # 나는 pop 시킬거라 while을 써야댕
    while que:
        current = que[0]

        # 맨앞 중요도보다 하나라도 큰게 있다면
        if current[0] < max(que)[0]:             # max다!!!!!
            que.append(que.popleft())
        # 아니야 제일 커
        else:
            # 근데 내가 찾는 애야
            if current[1] == -1:
                print(cnt+1)
                break
            # 아니야
            else:
                que.popleft()
                cnt += 1        


주석 제거ver

import sys
from collections import deque

k = int(sys.stdin.readline())
for _ in range(k):
    n, m = map(int, sys.stdin.readline().split())
    arr = list(map(int, sys.stdin.readline().split()))

    que = deque([[i,0] for i in arr])
    que[m][1] = -1                              # -1로 주는게 

    cnt = 0

    while que:
        if que[0][0] < max(que)[0]:             # max다!!!!!
            que.append(que.popleft())
        else:
            if que[0][1] == -1:
                print(cnt+1)
                break
            else:
                que.popleft()
                cnt += 1
profile
Do My Best

0개의 댓글