Blocks
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 4250   Accepted: 1704

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

Source

题意:

一排有颜色的方块,每次能够消除相邻的颜色同样的块,得分为方块个数的平方。消除后剩下的方块会合并,问如何消除方块使得总得分最大。

思路:

黑书原题(p123),合并初始相邻同样的块,得到颜色数组c和相应的长度len,dp[i][j][k]表示i~j区间,与后面k个同样颜色块一起消除得分的最大值(当然k个块的颜色必须与j同样),考虑len[j]和k这一段怎么消除,有两种可能:

1.单独消除,dp[i][j][k]=dp[i][j-1][0]+(len[j]+k)^2;

2.和前面的一起消除,如果前面的一起消除的块最后一块为p,那么dp[i][j][k]=dp[i][p][k+len[j]]+dp[p+1][j-1][0]。

能够依据p和j的颜色同样以及k的范围来优化一下。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define maxn 205
#define MAXN 200005
#define INF 0x3f3f3f3f
#define mod 1000000007
#define eps 1e-6
const double pi=acos(-1.0);
typedef long long ll;
using namespace std; int n,m,ans,tot;
int a[maxn],c[maxn],len[maxn],pos[maxn],last[maxn];
int dp[205][205][205],num[maxn][maxn]; void solve()
{
int i,j,k,p;
memset(pos,0,sizeof(pos));
for(i=1;i<=tot;i++)
{
last[i]=pos[c[i]];
pos[c[i]]=i;
}
memset(num,0,sizeof(num));
for(i=tot;i>=1;i--)
{
for(j=1;j<=n;j++)
{
if(j==c[i]) num[j][i]=num[j][i+1]+len[i];
else num[j][i]=num[j][i+1];
}
}
memset(dp,0,sizeof(dp));
for(int l=1;l<=tot;l++)
{
for(i=1;i<=tot;i++)
{
j=i+l-1;
if(j>tot) break ;
for(k=0;k<=num[c[j]][j+1];k++)
{
dp[i][j][k]=dp[i][j-1][0]+(len[j]+k)*(len[j]+k);
for(p=last[j];p>=i;p=last[p])
{
dp[i][j][k]=max(dp[i][j][k],dp[i][p][len[j]+k]+dp[p+1][j-1][0]);
}
}
}
}
ans=dp[1][tot][0];
}
int main()
{
int i,j,test,ca=0;
scanf("%d",&test);
while(test--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
tot=0;
memset(len,0,sizeof(len));
for(i=1;i<=n;)
{
tot++;
c[tot]=a[i];
while(i<=n&&a[i]==c[tot]) i++,len[tot]++;
}
solve();
printf("Case %d: %d\n",++ca,ans);
}
return 0;
}

poj 1390 Blocks (经典区间dp 方块消除)的更多相关文章

  1. POJ 1390 Blocks(记忆化搜索+dp)

    POJ 1390 Blocks 砌块 时限:5000 MS   内存限制:65536K 提交材料共计: 6204   接受: 2563 描述 你们中的一些人可能玩过一个叫做“积木”的游戏.一行有n个块 ...

  2. poj 1390 Blocks

    poj 1390 Blocks 题意 一排带有颜色的砖块,每一个可以消除相同颜色的砖块,,每一次可以到块数k的平方分数.问怎么消能使分数最大.. 题解 此题在徐源盛<对一类动态规划问题的研究&g ...

  3. Blocks题解(区间dp)

    Blocks题解 区间dp 阅读体验...https://zybuluo.com/Junlier/note/1289712 很好的一道区间dp的题目(别问我怎么想到的) dp状态 其实这个题最难的地方 ...

  4. UVA10559 方块消除 Blocks(区间dp)

    一道区间dp好题,在GZY的ppt里,同时在洛谷题解里看见了Itst orz. 题目大意 有n个带有颜色的方块,没消除一段长度为 \(x\) 的连续的相同颜色的方块可以得到 \(x^2\) 的分数,用 ...

  5. POJ 1390 Blocks(区间DP)

    Blocks [题目链接]Blocks [题目类型]区间DP &题意: 给定n个不同颜色的盒子,连续的相同颜色的k个盒子可以拿走,权值为k*k,求把所有盒子拿完的最大权值 &题解: 这 ...

  6. POJ 1390 Blocks (区间DP) 题解

    题意 t组数据,每组数据有n个方块,给出它们的颜色,每次消去的得分为相同颜色块个数的平方(要求连续),求最大得分. 首先看到这题我们发现我们要把大块尽可能放在一起才会有最大收益,我们要将相同颜色块合在 ...

  7. POJ 1160 经典区间dp/四边形优化

    链接http://poj.org/problem?id=1160 很好的一个题,涉及到了以前老师说过的一个题目,可惜没往那上面想. 题意,给出N个城镇的地址,他们在一条直线上,现在要选择P个城镇建立邮 ...

  8. POJ 3280 Cheapest Palindrome (区间DP) 经典

    <题目链接> 题目大意: 一个由小写字母组成的字符串,给出字符的种类,以及字符串的长度,再给出添加每个字符和删除每个字符的代价,问你要使这个字符串变成回文串的最小代价. 解题分析: 一道区 ...

  9. POJ 1390 Blocks(DP + 思维)题解

    题意:有一排颜色的球,每次选择一个球消去,那么这个球所在的同颜色的整段都消去(和消消乐同理),若消去k个,那么得分k*k,问你消完所有球最大得分 思路:显然这里我们直接用二位数组设区间DP行不通,我们 ...

随机推荐

  1. Photoshop  cs6 快捷键命令大全

    工具箱(多种工具共用一个快捷键的可同时按[Shift]加此快捷键选取) 矩形.椭圆选框工具.单行单列选取工具 [M] 裁剪工具.透视.切片.透视裁剪工具 [C] 移动工具 [V] 套索.多边形套索.磁 ...

  2. OPEN SUSE LINUX

    1. 把中文界面变成英文界面 yast2->system->language: 主要语言: 美式英语US 2. 使用root用户默认登录 Ubuntu使用root登录        Ubu ...

  3. SQLite 字段数据类型

    一般数据采用的固定的静态数据类型,而SQLite采用的是动态数据类型,会根据存入值自动判断. SQLite具有以下五种数据类型: 1.NULL:空值. 2.INTEGER:带符号的整型,具体取决有存入 ...

  4. mybatis的模糊查询格式

    mybatis的模糊查询格式: <select id="xxx" parameterType="com.model.xxx" resultMap=&quo ...

  5. 【ACM】不要62 (数位DP)

    题目:http://acm.acmcoder.com/showproblem.php?pid=2089 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer).杭州交通管理局经常会扩充一些的士车牌照,新 ...

  6. ros move_base costmap 理解和实现动态窗口法避障

    以下大部分内容参考自 ros_by_example_hydro_volume_1.pdf local costmap 是怎么生成的?跟三维点云有什么关系? global costmap在没有全局地图下 ...

  7. AC日记——Little Elephant and Function codeforces 221a

    A - Little Elephant and Function 思路: 水题: 代码: #include <cstdio> #include <iostream> using ...

  8. Redis单机部署、添加开机自启、配置参数

    1.Redis简介 redis是使用C语言编写的开源的,支持网络,基于内存,可持久性的键值对存储数据库,2013年5月之前,Redis是最流行的键值对存储数据库,Redis采用内存数据集,支持多种数据 ...

  9. 【bzoj1087】【互不侵犯King】状压dp裸题(浅尝ACM-D)

    [pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=54329606 向大(hei)佬(e)势力学(di ...

  10. mysql如何用jsp代码进行数据库备份

    mysql如何用jsp代码进行数据库备份 //导出 String mysql="mysqldump -uroot -proot --opt databasename > d:/test ...