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()