목차

Dance Dance Revolution

ps
링크acmicpc.net/…
출처BOJ
문제 번호2342
문제명Dance Dance Revolution
레벨골드 3
분류

DP

시간복잡도O(n)
인풋사이즈n<=100,000
사용한 언어Python
제출기록30764KB / 304ms
최고기록304ms
해결날짜2021/10/08

풀이

코드

"""Solution code for "BOJ 2342. Dance Dance Revolution".

- Problem link: https://www.acmicpc.net/problem/2342
- Solution link: http://www.teferi.net/ps/problems/boj/2342

Tags: [DP]
"""

INF = float('inf')


def energy(cur_pos, next_pos):
    if cur_pos == next_pos:
        return 1
    if cur_pos == 0:
        return 2
    elif abs(cur_pos - next_pos) == 2:
        return 4
    else:
        return 3


def main():
    positions = [int(x) for x in input().split()]
    positions.pop()
    dp_cur = [INF] * 5
    dp_cur[0] = energy(0, positions[0])
    for prev_pos, cur_pos in zip(positions, positions[1:]):
        dp_prev = dp_cur
        # (prev_pos, pprev_pos) -> (cur_pos, pprev_pos)
        energy_from_prev = energy(prev_pos, cur_pos)
        dp_cur = [tot_energy + energy_from_prev for tot_energy in dp_prev]
        dp_cur[cur_pos] = INF
        # (prev_pos, pprev_pos) -> (prev_pos, cur_pos)
        if prev_pos != cur_pos:
            dp_cur[prev_pos] = min(
                tot_energy + energy(pprev_pos, cur_pos)
                for pprev_pos, tot_energy in enumerate(dp_prev))

    print(min(dp_cur))


if __name__ == '__main__':
    main()