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

Updated:

Answer

from collections import deque
import sys

input = sys.stdin.readline

n, m  = map(int, input().split())
a = list(map(int, input().split()))
d = deque([i for i in range(1, n + 1)])

cnt = 0

for i in a:
    while True:
        if d[0] == i:
            d.popleft()
            break
        elif len(d) - d.index(i) > d.index(i):
            cnt += 1
            d.append(d.popleft())
        else:
            cnt += 1
            d.appendleft(d.pop())

print(cnt)

Categories:

Updated: