알고리즘/백준

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

알감자 2021. 12. 28. 20:49
 

1021번: 회전하는 큐

첫째 줄에 큐의 크기 N과 뽑아내려고 하는 수의 개수 M이 주어진다. N은 50보다 작거나 같은 자연수이고, M은 N보다 작거나 같은 자연수이다. 둘째 줄에는 지민이가 뽑아내려고 하는 수의 위치가

www.acmicpc.net

 

코드

n, m = map(int, input().split())
want_list = list(map(int, input().split()))

num_list = [i+1 for i in range(n)]

cnt = 0
answer = []
for i in range(m):
    if num_list.index(want_list[i]) >= len(num_list)/2:
        while num_list[0] != want_list[i]:
            x= num_list.pop()
            num_list.insert(0,x)
            cnt += 1
    else:
        while num_list[0] != want_list[i]:
            x = num_list.pop(0)
            num_list.append(x)
            cnt += 1
    answer.append(num_list.pop(0))

print(cnt)