코딩테스트/백준(BOJ)

백준 - 회전하는 큐(1021번) - 파이썬(python)

진한색 2022. 12. 24. 16:18

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

 

1021번: 회전하는 큐

첫째 줄에 큐의 크기 N과 뽑아내려고 하는 수의 개수 M이 주어진다. N은 50보다 작거나 같은 자연수이고, M은 N보다 작거나 같은 자연수이다. 둘째 줄에는 지민이가 뽑아내려고 하는 수의 위치가

www.acmicpc.net

 

파이썬 코드

 

from collections import deque
import sys
input =sys.stdin.readline

n,m=map(int,input().split())

ids=list(map(int,input().split()))

bq=deque([i for i in range(1,n+1)])

cnt=0

for idx in ids:
    while 1:
        if bq[0]==idx:
            bq.popleft()
            break
        else:
            if bq.index(idx)<len(bq)/2:
                while bq[0]!=idx:
                    bq.append(bq.popleft())
                    cnt+=1
            else:
                while bq[0]!=idx:
                    bq.appendleft(bq.pop())
                    cnt+=1
                   
print(cnt)
728x90