Baekjoon #1021

Baekjoon #1021

1개의 풀이 · Python

문제 원문 보기 ↗

SOLUTION INFO

Python · main.py

main.py
import sys
from collections import deque
def input():
    return sys.stdin.readline().rstrip()

N, M = map(int, input().split())
arr = list(map(int, input().split()))
queue = deque([i for i in range(1,N+1)])
ans = 0
for i in arr:
    idx = queue.index(i)
    if idx == 0:
        queue.popleft()
    else:
        if idx <= len(queue)//2:
            queue.rotate(-idx)
            queue.popleft()
            ans += idx
        else:
            queue.rotate(len(queue) - idx)
            ans += len(queue) - idx
            queue.popleft()
print(ans)

SOLUTION DESCRIPTION

풀이 설명

등록된 풀이 설명이 없습니다.