๋ฐฑ์ค€ 1021๋ฒˆ ํŒŒ์ด์ฌ

2021. 4. 2. 21:36ใ†Python

www.acmicpc.net/problem/1021

 

1021๋ฒˆ: ํšŒ์ „ํ•˜๋Š” ํ

์ฒซ์งธ ์ค„์— ํ์˜ ํฌ๊ธฐ N๊ณผ ๋ฝ‘์•„๋‚ด๋ ค๊ณ  ํ•˜๋Š” ์ˆ˜์˜ ๊ฐœ์ˆ˜ M์ด ์ฃผ์–ด์ง„๋‹ค. N์€ 50๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๊ณ , M์€ N๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ์ง€๋ฏผ์ด๊ฐ€ ๋ฝ‘์•„๋‚ด๋ ค๊ณ  ํ•˜๋Š” ์ˆ˜์˜ ์œ„์น˜๊ฐ€

www.acmicpc.net

์ด ๋ฌธ์ œ๋Š” index๋งŒ ์“ฐ๋ฉด ๊ธˆ๋ฐฉ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

์ฒ˜์Œ์—๋Š” ๊ทธ๋ƒฅ ํ์˜ ๊ฐ€์žฅ ์•ž ์›์†Œ(q[0])์™€ ๋น„๊ตํ•ด์„œ ํ•ด๊ฒฐํ•˜๋„๋ก ์ฝ”๋“œ๋ฅผ ์งฐ๋”๋‹ˆ ์ตœ์†Œ๊ฐ’์ด ์•„๋‹ˆ๋ผ ์ตœ๋Œ€๊ฐ’์— ๊ฐ€๊นŒ์šด ๊ฐ’์„ ๊ณ„์‚ฐํ•˜๊ณ  ์žˆ๋‹ค๋Š” ๊ฑธ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•˜๋ฉด ์ข‹์„๊นŒ, ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ popํ•˜๊ณ ์ž ํ•˜๋Š” ์ˆซ์ž์˜ ์•ž๊ณผ ๋’ค์˜ ๊ธธ์ด๋ฅผ ๋น„๊ตํ•ด ์งง์€ ์ชฝ์œผ๋กœ ์—ฐ์‚ฐ(๋‘๋ฒˆ์งธ or ์„ธ๋ฒˆ์งธ)์„ ํ•˜๋ฉด ๊ธˆ๋ฐฉ ํ•ด๊ฒฐ๋œ๋‹ค๋Š” ๊ฑธ ์•Œ์•˜๋‹ค.

 

index ํ•จ์ˆ˜๋Š” ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(1)์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ดœํžˆ ๊ฑฑ์ •ํ•  ํ•„์š” ์—†๋‹ค!

 

<๊ฒฐ๊ณผ ์ฝ”๋“œ>

from sys import stdin
from collections import deque

def firstFunc():
    q.popleft()

def secondFunc(second):
    q.append(q.popleft())
    return second + 1

def thirdFunc(third):
    q.appendleft(q.pop())
    return third + 1


q = deque()
N, M = map(int, stdin.readline().split())
wantToPop=list(map(int, stdin.readline().split()))
for i in range(1,N+1): q.append(i)

second = 0
third = 0

for number in wantToPop:
    while number != q[0]:
        if q.index(number) <= len(q)//2:
            second = secondFunc(second)
        elif q.index(number) > len(q)//2:
            third = thirdFunc(third)
    if number == q[0]: firstFunc()

print(second + third)