[动态规划]Pku1390 Blocks
来源:互联网 发布:下载超牛数据恢复软件 编辑:程序博客网 时间:2024/06/07 02:32
Description
The corresponding picture will be as shown below:
Figure 1
If some adjacent boxes are all of the same color, and both the box to its left(if it exists) and its right(if it exists) are of some other color, we call it a 'box segment'. There are 4 box segments. That is: gold, silver, bronze, gold. There are 1, 4, 3, 1 box(es) in the segments respectively.
Every time, you can click a box, then the whole segment containing that box DISAPPEARS. If that segment is composed of k boxes, you will get k*k points. for example, if you click on a silver box, the silver segment disappears, you got 4*4=16 points.
Now let's look at the picture below:
Figure 2
The first one is OPTIMAL.
Find the highest score you can get, given an initial state of this game.
Input
Output
Sample Input
291 2 2 2 2 3 3 3 111
Sample Output
Case 1: 29Case 2: 1
Source
题目大意:请参看刘汝佳的《算法艺术与信息学奥赛》。
题解:
题目的方块可以表示成color[i],len[i],1 <= i <= l,这里l表示有多少“段”不同的颜色方块。color[i]表示第i段的颜色,len[i]表示第i段的方块长度。让f[i][j][k]表示把(color[i], len[i]),(color[i + 1],len[i + 1]……(color[j], len[j] + k)合并的最大得分。
考虑(color[j], len[j] + k)这一段,要不马上消掉,要不和前面的若干段一起消。
如果马上消掉,就是f[i][j][0] + (len[j] + k) ^ 2
如果和前面若干段一起消,可以假设这“若干段”中最后面的那一段是a,则此时得分是f[i][a][len[j]] + f[a + 1][j - 1][0].
f[i][j][k] = max(f[i][j - 1][0] + (len[j] + k )^2, f[i][a][k + len[j]] + f[a + 1][j - 1][0] }
其中color[a] = color[j].
理解:这种线性dp思想和剖分类dp类似,只不过本题需要添加一个域来考虑这种类型的情况:
1 1 1 x x x 1 1 1,将后面的111和前面的111一起合并时,得分是6*6,若没有第三个域k来表示后面还有多少个方块,就没办法统计得分。实现这个dp可以用记忆化搜索,当然也可以类似剖分地写,不过记忆化搜索容易实现很多。
codes:
- [动态规划]Pku1390 Blocks
- POJ1390 Blocks 【动态规划】
- poj 1609 Tiling Up Blocks 动态规划
- POJ 3734 Blocks (动态规划) (矩阵幂)
- poj 动态规划DP - 1609 Tiling Up Blocks
- poj 1390 Blocks(记忆化搜索/动态规划)
- 简单动态规划(POJ 1609 Tiling Up Blocks 解题报告)
- poj 3734 Blocks (矩阵快速幂优化的动态规划)
- codeforces621E————Wet Shark and Blocks(动态规划,矩阵快速幂)
- 100道动态规划——34 UVA 10559 Blocks 状态的定义 状态转移方程
- 动态规划!!!动态规划!!!
- 动态规划
- 动态规划
- 动态规划
- 动态规划
- 动态规划
- 动态规划
- 动态规划
- 请大家支持
- 编写代码注意事项
- IIC学习笔记
- 日文电子邮件的格式礼仪
- C# 设计模式学习二 Singleton单例模式
- [动态规划]Pku1390 Blocks
- 批处理运行多个程序的方法/如何写批处理运行程序
- Mathtype输入Latex模式公
- ビジネス文書ー挨拶状
- 多重继承和虚基类
- 敬语和谦语
- 书信-例子
- 纪念第一次发技术铁 拉回来瞧瞧 关于int 9
- 商务文书范例——邀请函、请柬