from collections import deque
# N: 큐의 크기, M: 뽑으려는 숫자의 개수
n, m = map(int, input().split())
# 뽑아내려는 수의 위치들
targets = list(map(int, input().split()))
# 1부터 N까지 숫자가 담긴 큐 생성
queue = deque(range(1, n + 1))
total_moves = 0
for target in targets:
while True:
# 원하는 숫자가 맨 앞에 오면 바로 제거
if queue[0] == target:
queue.popleft()
break
else:
# 타겟의 현재 인덱스를 찾음
idx = queue.index(target)
# 큐의 현재 크기의 절반보다 앞에 있는지 확인
if idx <= len(queue) // 2:
# 왼쪽으로 회전 (2번 연산)
queue.append(queue.popleft())
total_moves += 1
else:
# 오른쪽으로 회전 (3번 연산)
queue.appendleft(queue.pop())
total_moves += 1
print(total_moves)
To embed this project on your website, copy the following code and paste it into your website's HTML: