博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj1390 (区间dp)
阅读量:4943 次
发布时间:2019-06-11

本文共 2591 字,大约阅读时间需要 8 分钟。

Description

Some of you may have played a game called ‘Blocks’. There are n blocks in a row, each box has a color. Here is an example: Gold, Silver, Silver, Silver, Silver, Bronze, Bronze, Bronze, Gold.
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

The first line contains the number of tests t(1<=t<=15). Each case contains two lines. The first line contains an integer n(1<=n<=200), the number of boxes. The second line contains n integers, representing the colors of each box. The integers are in the range 1~n.

Output

For each test case, print the case number and the highest possible score.

Sample Input

2 9 1 2 2 2 2 3 3 3 1 1 1

Sample Output

Case 1: 29 Case 2: 1

分析:

显然,一样颜色的一段区域要一起消,这样贡献才最大
我们可以把一段颜色相同的区域缩成一个点
(后来证明没必要)

我一开始的方程是这样的:

f[i][j] 表示消掉i~j的最大值
如果i和j的颜色一样,那么f[i][j]=f[i+1][j-1]+(len[i]+len[j])^2
否则f[i][j]=max{f[i][k]+f[k+1][j]}

但是可能出现这样的状态:

11221113331
很显然,先消掉2和3,之后一起消掉1才最优
但是要是按照我的转移状态,
只会是消掉22111333之后再消掉最边上的1

所以我们就要改变一下转移的状态:

f[i][j][k] i~j消到只剩下k个,

规定这k个块和i,j的颜色一样

g[i][j] i~j全部消掉的最大值

f[i][j][k]=max{f[i][l][k-1]+g[l+1][j-1]}

表示把l+1~j-1段完全消除,使j与前面颜色相同的k-1个合并,

注意枚举的l必须与i,j颜色相同

**这里写图片描述**

g[i][j]=max(g[i][l]+g[l+1][j],f[i][j][k]+k^2)

可以将i~j从中间断开分别消除,也可以将i~j剩下的k个一次消除

tip

g[i][j]=g[i][l]+g[l+1][j]

不牵扯到k,所以可以单独拿出来转移

重申原则

一定要从稳定状态转移

我写出来之后发现跑出来的答案比标准答案大

这时候我发现题解上有一句话
memset(f,128,sizeof(f)); //初始值为-INF
我一开始是把所有f都附成了0
改过来之后就好了

这里写代码片#include
#include
#include
using namespace std;int n;int a[202],tot;int f[202][202][202],g[202][202];int doit(){ int i,j,k,l; memset(f,128,sizeof(f)); memset(g,0,sizeof(g)); for (i=1;i<=n;i++) g[i][i]=1,f[i][i][0]=0,f[i][i][1]=0; for (i=n-1;i>=1;i--) for (j=i+1;j<=n;j++) { if (a[i]==a[j]) //col must be same for (k=1;k<=j-i+1;k++) { for (l=i;l

转载于:https://www.cnblogs.com/wutongtong3117/p/7673201.html

你可能感兴趣的文章
移动终端app测试点总结
查看>>
14-6-27&28自学内容小结
查看>>
JSP
查看>>
---
查看>>
(第一组_GNS3)自反ACl
查看>>
hdu--1258--Sum It Up(Map水过)
查看>>
Spring @DeclareParents 的扩展应用实例
查看>>
VS2012更新Update1后帮助查看器无法打开
查看>>
Android 文件的读取和写入
查看>>
高校表白APP-冲刺第四天
查看>>
outlook 设置163邮箱
查看>>
mysql优化——show processlist命令详解
查看>>
Solr服务器搭建
查看>>
画世界怎么用光影_世界绘画经典教程:水彩光影魔法教程
查看>>
win+rsync+php,跨平台的fswatch+rsync同步备份
查看>>
vue2 cdn 加载html,vue项目中使用CDN加载
查看>>
github.com访问慢解决
查看>>
转:哈夫曼树详解
查看>>
.Net Core Identity外面使用Cookie中间件
查看>>
C#中泛型之Dictionary
查看>>