|
| 1 | +# 백준 2240번: 자두나무 (2회차) |
| 2 | + |
| 3 | +import sys |
| 4 | + |
| 5 | +input = sys.stdin.readline |
| 6 | + |
| 7 | +# T <= 10^3 |
| 8 | +# W <= 30 |
| 9 | +T, W = map(int, input().rstrip().split()) |
| 10 | + |
| 11 | +# 관찰 |
| 12 | +# 누가 봐도 DP 문제 |
| 13 | +# 최초에는 1번에 위치 |
| 14 | +# 매 초마다 이동하는 경우와 이동하지 않는 경우의 자두 먹은 개수를 세야 할 듯 |
| 15 | +# DP 점화식 |
| 16 | +# dp[i][j][k] = i초에 j번 나무 밑에 k번 이동해서 도착했을 때 먹을 수 있는 최대 자두 개수? |
| 17 | +# 1차 접근 후 피드백 |
| 18 | +# j는 k가 홀수, 짝수인지에 따라서 자동으로 결정되는 변수임, 나무가 2개뿐이기 때문 |
| 19 | +# 개선된 DP 점화식 |
| 20 | +# dp[i][j] = i초에 j번 이동해서 도착했을 때 먹을 수 있는 최대 자두 개수 |
| 21 | + |
| 22 | +dp = [[0 for _ in range(W + 1)] for _ in range(T + 1)] |
| 23 | +dp[0][0] = 0 |
| 24 | + |
| 25 | +def print_dp(plum_drop): |
| 26 | + print(' O' if plum_drop == 1 else ' O') |
| 27 | + for idx, row in enumerate(dp[1:]): |
| 28 | + print(f' {idx + 1}초', row) |
| 29 | + |
| 30 | +for i in range(1, T + 1): |
| 31 | + plum_drop = int(input()) |
| 32 | + # print(f'{i}초, {plum_drop}번 나무에서 떨어짐') |
| 33 | + |
| 34 | + for j in range(min(i + 1, W + 1)): |
| 35 | + current_tree = j % 2 + 1 |
| 36 | + |
| 37 | + if j == 0: |
| 38 | + if current_tree == plum_drop: |
| 39 | + dp[i][j] = dp[i - 1][j] + 1 |
| 40 | + else: |
| 41 | + dp[i][j] = dp[i - 1][j] |
| 42 | + continue |
| 43 | + |
| 44 | + if current_tree == plum_drop: |
| 45 | + dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1]) + 1 |
| 46 | + else: |
| 47 | + dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1]) |
| 48 | + |
| 49 | + # print_dp(plum_drop) |
| 50 | + # print() |
| 51 | + |
| 52 | +print(max(dp[T])) |
0 commit comments