高级农民
- 积分
- 2331
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2020-5-3
- 最后登录
- 1970-1-1
|
本帖最后由 VXZ 于 2021-1-1 02:28 编辑
加米必回加 做了几道DP:paint fence 需要定义两个DP数组
same means the number of ways if fence i has the same color with fence i - 1.
diff[i] means the number of ways for the fence i which has different color with fence i -1.
Initialization:
same[0] = 0, diff[0] = k.
Induction rule:
Case 1 : if paint i same color with i - 1, [/i][i]i - 1 must be different from i - 2, then only use diff[i - 1] to computer same [/i][/i][i][i][i] same[i][i]= diff[i - 1] [/i][/i]
[i][i] Case 2: if paint i different color with i - 1, doesn't matter if color i - 1 same or different with i - 2, can use both[/i][/i]
[i][i] there are (dp1[i - 1] + dp2[i - 1]) # of ways to paint previous i - 1, [/i][/i][i][i]for each choice, (k - 1) ways to paint current fence[/i][/i]
[i][i] diff[i] [i]= (k - 1) * (same[i - 1] + diff[i - 1])[/i][/i][/i]
[/i][/i][/i]
|
-
1.png
(98.15 KB, 下载次数: 0)
|