from collections import deque
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
p = list(map(int, input().split()))
dq = deque([i for i in range(1, n+1)])
cnt = 0
for i in p:
while True:
if dq[0] == i:
dq.popleft()
break
else:
if dq.index(i) < len(dq)/2:
while dq[0] != i:
dq.append(dq.popleft())
cnt += 1
else:
while dq[0] != i:
dq.appendleft(dq.pop())
cnt += 1
print(cnt)
덱에 들어있는 수보다 내가 찾고자 하는 수의 위치가 왼쪽에 있을 땐 popleft를 해주고
덱에 들어있는 수보다 내가 찾고자 하는 수의 위치가 오른쪽에 있을 땐 pop 해준다.
'문제풀이 > 백준(Boj) 문제풀이' 카테고리의 다른 글
[백준][BFS] 1697. 숨박꼭질(파이썬/Python) (0) | 2021.12.13 |
---|---|
[백준][BFS] 1012. 유기농 배추 (파이썬/Python) (0) | 2021.12.12 |
[백준][큐 & 덱] 5430. AC (파이썬/Python) (0) | 2021.12.07 |
[백준][큐 &덱] 10886. 덱 (파이썬/Python) (0) | 2021.12.07 |
[백준][큐 & 덱] 1966. 프린터 큐 (파이썬/Python) (0) | 2021.12.06 |