๋ฐฑ์ค€ 2342 Dance Dance Revolution

passยท2022๋…„ 5์›” 22์ผ
0

์•Œ๊ณ ๋ฆฌ์ฆ˜

๋ชฉ๋ก ๋ณด๊ธฐ
11/35

๋ฌธ์ œ

๐Ÿ‘€ ๋ฌธ์ œ ์‚ฌ์ดํŠธ : https://www.acmicpc.net/problem/2342






ํ’€์ด

๋‚œ์ด๋„ : Gold III

์ด ๋ฌธ์ œ๋ฅผ ์ฒ˜์Œ ํ’€ ๋•Œ๋Š” greedy๋กœ ๋ชจ๋“  ๊ณณ์„ ํƒ์ƒ‰ํ•˜๋ฉด์„œ q์— ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋Š˜๋ ค๊ฐ€๋ฉด์„œ ํ•ด๊ฒฐํ•˜๋ ค๊ณ  ํ•˜์˜€๋‹ค.
๊ทธ๋ ‡๊ฒŒ ํ’€์—ˆ์„ ๊ฒฝ์šฐ์— ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ๋งŽ์ด ํ•ด์ฃผ์–ด์•ผ ๋˜๊ณ , step์€ ๋˜‘๊ฐ™์€๋ฐ ๊ฐ’๋งŒ ๋‹ค๋ฅธ ๊ฒƒ๋“ค์ด ์ค‘๋ณต๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•˜์˜€์œผ๋ฉฐ, ๊ฒฐ๋ก ์ ์œผ๋กœ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๋‹ค.

๊ทธ๋ž˜์„œ dp๋ฅผ ์ด์šฉํ•˜์—ฌ ํ’€๊ฒŒ ๋˜์—ˆ๊ณ , 3์ค‘ array๋กœ dp๋ฅผ ๊ตฌ์„ฑํ•˜์—ฌ bottom up ํ˜•์‹์œผ๋กœ ํ’€์—ˆ๋‹ค.

instruction์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๊ฐ€ 100000์ด๋ฏ€๋กœ, instruction ํ•˜๋‚˜๋‹น 1๋ฒˆ,2๋ฒˆ,3๋ฒˆ,4๋ฒˆ์นธ๊ณผ ์™ผ๋ฐœ, ์˜ค๋ฅธ๋ฐœ์˜ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ƒ๊ฐํ•ด๋ณด์•„๋„ ์‹œ๊ฐ„์ดˆ๊ณผ์—†์ด ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋ผ ์ƒ๊ฐํ•˜์˜€๋‹ค.

  • dp[a][b][c]
    • a๋Š” instruction index๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
    • b๋Š” left ์œ„์น˜๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
    • c๋Š” right ์œ„์น˜๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

1) instruction์˜ ๋งˆ์ง€๋ง‰์€ 0์ด ์ž…๋ ฅ๋˜๋ฏ€๋กœ n ์„ len(ins) - 1 ๋กœ ๊ตฌ์„ฑํ•˜์—ฌ 0์€ ์ฐธ์กฐํ•˜์ง€ ์•Š๋„๋ก ํ•ด์ค€๋‹ค.
2) move๋ผ๋Š” ํ•จ์ˆ˜๋Š” ๋ฌธ์ œ์— ๊ตฌํ˜„๋˜์–ด ์žˆ๋Š” ์ด๋™ํ•  ๋•Œ ๋“œ๋Š” ํž˜์„ ๊ทธ๋Œ€๋กœ ๊ตฌํ˜„ํ•œ๋‹ค.
3) ๋ชจ๋“  instruction์— ๋Œ€ํ•˜์—ฌ bottom up์œผ๋กœ dp๋ฅผ ์ฑ„์›Œ๋‚˜๊ฐ„๋‹ค.

๐Ÿ‘‰ ํ˜„์žฌ ์ฑ„์›Œ์ ธ์žˆ๋Š” dp์ƒํƒœ์™€ ์ „ ๋‹จ๊ณ„(i-1)์—์„œ ์ด๊ณณ์œผ๋กœ ์ด๋™ํ•ด์™”์„ ๋•Œ ๊ฐ’์„ ๋น„๊ตํ•˜์—ฌ ์ž‘์€ ๊ฐ’์œผ๋กœ dp ๊ฐ’์„ ๋‹ค์‹œ ์ •ํ•ด์ค€๋‹ค.
(์ฒ˜์Œ ์ƒํƒœ์—์„œ i-1๋กœ ์ฐธ์กฐํ–ˆ์„ ๋•Œ๋ฅผ ๋Œ€๋น„ํ•ด ์‹œ์ž‘ ์ „์— dp[-1][0][0] = 0 ์œผ๋กœ ์กฐ์น˜๋ฅผ ํ•ด๋‘์—ˆ์Œ)






์ฝ”๋“œ

ins = list(map(int, input().split()))
n = len(ins)-1
dp = [[[400001 for _ in range(5)] for _ in range(5)] for _ in range(n+1)]
dp[-1][0][0] = 0

def move(start, end):
    if start == 0:
        if end == 0:
            return 0
        else:
            return 2

    if start == end:
        return 1
    elif abs(start - end) == 1 or abs(start - end) == 3:
        return 3
    else:
        return 4

for i in range(n):
    for current in range(5):
        for start in range(5):
            add = move(start, ins[i])
            # ์™ผ๋ฐœ ์ด๋™
            dp[i][ins[i]][current] = min(dp[i][ins[i]][current], dp[i-1][start][current] + add)
            # ์˜ค๋ฅธ๋ฐœ ์ด๋™
            dp[i][current][ins[i]] = min(dp[i][current][ins[i]], dp[i-1][current][start] + add)

result = int(1e9)
for l in range(5):
    for r in range(5):
        result = min(result, dp[n-1][l][r])
print(result)
profile
์•ˆ๋“œ๋กœ์ด๋“œ ๊ฐœ๋ฐœ์ž ์ง€๋ง์ƒ

0๊ฐœ์˜ ๋Œ“๊ธ€