백준

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

MC류짱 2023. 5. 22. 12:58

풀이

import sys
from collections import deque


def main():
    N, M = map(int, sys.stdin.readline().strip().split())
    lst = list(map(int, sys.stdin.readline().strip().split()))

    Q = deque(range(1, N+1))

    rst = 0

    for num in lst:
        lst_len = len(Q)
        num_idx = Q.index(num)
        if num_idx <= (lst_len//2):
            while Q[0] != num:
                Q.append(Q.popleft())
                rst += 1
            Q.popleft()
        else:
            while Q[0] != num:
                Q.appendleft(Q.pop())
                rst += 1
            Q.popleft()

    print(rst)


if __name__ == '__main__':
    main()