[动态规划]Pku1390 Blocks
2009-12-26 15:16
246 查看
Blocks
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
Sample Output
Source
Liu Rujia@POJ
题目大意:请参看刘汝佳的《算法艺术与信息学奥赛》。
题解:
题目的方块可以表示成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:
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 1497 | Accepted: 567 |
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
Source
Liu Rujia@POJ
题目大意:请参看刘汝佳的《算法艺术与信息学奥赛》。
题解:
题目的方块可以表示成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:
var t,n,m,i:longint; c,l:array[0..200] of longint; f:array[0..200,0..200,0..200] of longint; procedure init; var i,x:longint; begin readln(n); m:=0; for i:=1 to n do begin read(x); if x=c[m] then inc(l[m]) else begin inc(m); c[m]:=x; l[m]:=1; end; end; fillchar(f,sizeof(f),255); for i:=1 to m do f[i,i-1,0]:=0; end; function max(a,b:longint):longint; begin if a<b then a:=b; max:=a; end; function dp(x,y,k:longint):longint; var i:longint; begin if f[x,y,k]>=0 then exit(f[x,y,k]); if x=y then exit((l[x]+k)*(l[x]+k)); f[x,y,k]:=dp(x,y-1,0)+(l[y]+k)*(l[y]+k); for i:=x to y-1 do if c[i]=c[y] then f[x,y,k]:=max(f[x,y,k],dp(x,i,l[y]+k)+dp(i+1,y-1,0)); exit(f[x,y,k]); end; begin readln(t); for i:=1 to t do begin dec(t); init; writeln('Case ',i,': ',dp(1,m,0)); end; end.
相关文章推荐
- POJ1390 Blocks——动态规划——pku1390
- POJ1390 Blocks 【动态规划】
- POJ1390 Blocks 【动态规划】
- 【DP+记忆化搜索】PKU-1390-Blocks
- pku 1390 Blocks(DP)
- poj-1390-Blocks(方块消除)-DP
- poj 1390 Blocks(记忆化搜索/动态规划)
- hrbeu 放苹果 pku 1664 动态规划
- POJ 1390 Blocks(DP)
- poj 1390 Blocks
- Pku acm 2250 Compromise 动态规划题目解题报告(六)
- PKU1080--Human Gene Functions(基因相似度,动态规划)
- pku3638 Moogle (动态规划)
- pku3211 Washing Clothes(动态规划)
- [poj1390]Blocks(方块消除)
- POJ 1390 Blocks (DP)
- 简单动态规划(POJ 1609 Tiling Up Blocks 解题报告)
- POJ-1390方盒游戏(动态规划)
- hdu 1688 Sightseeing (次短路及次短路数)&&pku3255 Roadblocks &&3191 How Many Paths Are There
- pku2385 Apple Catching (动态规划)