๐๏ธ Algorithm/๐ฉ ๋ฐฑ์ค
๐ฉ [๋ฐฑ์ค] [Python] [ํ] 16927๋ฒ_๋ฑ๊ณผ ์ฌ๋ค๋ฆฌ ๊ฒ์
Dbswnstjd
2022. 11. 18. 18:52
๋ฌธ์
https://www.acmicpc.net/problem/1021
1021๋ฒ: ํ์ ํ๋ ํ
์ฒซ์งธ ์ค์ ํ์ ํฌ๊ธฐ N๊ณผ ๋ฝ์๋ด๋ ค๊ณ ํ๋ ์์ ๊ฐ์ M์ด ์ฃผ์ด์ง๋ค. N์ 50๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์ ์์ฐ์์ด๊ณ , M์ N๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์ ์์ฐ์์ด๋ค. ๋์งธ ์ค์๋ ์ง๋ฏผ์ด๊ฐ ๋ฝ์๋ด๋ ค๊ณ ํ๋ ์์ ์์น๊ฐ
www.acmicpc.net
ํ์ด
# ๋ฐฑ์ค 1021๋ฒ ๋ฌธ์ - ํ์ ํ๋ ํ
from collections import deque
n,m = list(map(int,input().split()))
value = list(map(int,input().split()))
q = deque([i+1 for i in range(n)])
count = 0
for x in value:
while True:
if q.index(x) == 0:
q.popleft() # 1๋ฒ ๋ก์ง
break
# ์์น 0๊ณผ์ ๊ฑฐ๋ฆฌ ์ฐจ์ด๋ก ์ผ์ชฝ์ผ๋ก ์ด๋ํ ์ง ์ค๋ฅธ์ชฝ์ผ๋ก ์ด๋ํ ์ง๋ฅผ ๊ฒฐ์
if q.index(x) <= len(q) - q.index(x): # 2๋ฒ ์ผ์ชฝ์ผ๋ก ์ด๋ํ๊ธฐ ๋ก์ง
q.append(q.popleft())
count += 1
else:
q.appendleft(q.pop()) # 3๋ฒ ์ค๋ฅธ์ชฝ์ผ๋ก ์ด๋ํ๊ธฐ ๋ก์ง
count += 1
print(count)