AGC 26 D Histogram Coloring
将柱子的高度离散化\(\DeclareMathOperator{\dp}{dp}\)
设第 \(i\) 根柱子实际高度是 \(h_i\),离散化之后的高度是 \(g_i\);第 \(i\) 高的高度是 \(H_i\),第 \(i\) 段的长度为 \(c_i\),即 \(c_0 = H_0,c_i = H_i - H_{i-1} \quad i \ge 1\)
设有三根柱子,高度分别为 \(1, 4, 3\),则 \(h = [1, 4, 3]\),\(g = [0, 2, 1]\),$ H = [1, 3, 4]\(,\)c = [1, 2, 1]$ 。
\(\dp[i][j]\) 表示第 \(i\) 根柱子上「上下相邻的两块同色」最早出现在第 \(j\) 段的方案数。
第 \(i\) 根柱子上未出现相邻两块同色的情况用状态 \(\dp[i][g_i+1]\) 表示
转移方程
\(\dp[i][j]\)
- \(g_{i-1} \ge j\):\(\dp[i][j] = \dp[i-1][j] \times 2^{\max(0, h_i - h_{i-1})}\)
- \(g_{i-1} < j\):\(\dp[i][j] = \dp[i-1][g_{i-1} + 1] \times 2 \times (2^{c_j} -1) \times 2^{h_{i} - H_{j}}\)
边界条件
\(\dp[0][0] = (2^{c_0} - 2) \times 2^{h_0 - H_0}\)
\(\dp[0][j] = 2 \times (2^{c_j} - 1) \times 2^{h_0 - H_j} \quad 1 \le j \le g_0\)
\(\dp[0][g_0 + 1] = 2\)
\(\dp[i][g_i + 1]\)
- \(g_{i-1} \le g_i\):\(\dp[i][g_i+1] = 2 \times \dp[i-1][g_{i-1} + 1]\)
- \(g_{i-1} > g_i\):\(\dp[i][g_i + 1] = 2 \times \sum_{ g_i < j \le g_{i-1} + 1} \dp[i-1][j]\)