๐Ÿ—๏ธ 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)