Dynamic Programming21 sections · 916 units
Open in Course

Codeforces 478D Red Green Towers - Transition

Color choice

Level ii needs exactly ii blocks, all one color. From state dp[i1][j]dp[i-1][j] (built i1i-1 levels, used jj red), you have two choices:

1.1. Make level ii red: Use ii more red blocks. Go to dp[i][j+i]dp[i][j + i].

2.2. Make level ii green: Use ii green blocks. Go to dp[i][j]dp[i][j]. The transition (how we move between states) adds the number of ways: dp[i][j]+=dp[i1][j]dp[i][j] += dp[i-1][j] (green) and dp[i][j+i]+=dp[i1][j]dp[i][j+i] += dp[i-1][j] (red). This transition captures the relationship between subproblems.