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. 最简单的基于FFmpeg的AVDevice例子(读取摄像头)【转】

    转自:http://blog.csdn.net/leixiaohua1020/article/details/39702113 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[- ...

  2. [Oracle] DataGuard switchover

    Oracle DataGuard switchover 2013/07/11 Tag.Data Guard,primary,standby,switchover 切换前primary site和sta ...

  3. 信号槽库:sigslot.h和sigc++使用

    用qt的知道,qt有方便简单的信号槽机制,但需要专门的qt工具处理. 如果想直接使信号槽就可以使用sigslot库,或者sigc++库,或者boost中的signals,这里介绍sigslot和sig ...

  4. Linux C/C++内存泄漏检测工具:Valgrind

    Valgrind 是一款 Linux下(支持 x86.x86_64和ppc32)程序的内存调试工具,它可以对编译后的二进制程序进行内存使用监测(C语言中的malloc和free,以及C++中的new和 ...

  5. ObjectInputStream&ObjectOutputStream工具类

    序列化:将数据保存到文件:ObjectOutputStream; 反序列化:将文件中的数据显示出来:ObjectInputStream;   在反序列化程序中运行后能够正常输出Person的相关信息, ...

  6. Good Bye 2016 A. New Year and Hurry【贪心/做题目每道题花费时间按步长为5等差增长,求剩余时间够做几道题】

    A. New Year and Hurry time limit per test 1 second memory limit per test 256 megabytes input standar ...

  7. Codeforces Round #447 (Div. 2) A. QAQ【三重暴力枚举】

    A. QAQ time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

  8. flow JavaScript 静态类型检查工具

    内置类型 flow 内置类型有 boolean, number, string, null, void, any, mixed, literal type. 其中 boolean, number, s ...

  9. #423 Div2 D

    #423 Div2 D 题意 构造一个 n 个节点的树,恰好有 k 个叶子节点 (叶子节点的定义是只与树上的某一个节点存在连边),要求任意两个叶子节点的距离的最大值最小,距离为两个节点间边的数量,输出 ...

  10. Python爬取中国天气网

    Python爬取中国天气网 基于requests库制作的爬虫. 使用方法:打开终端输入 “python3 weather.py 北京(或你所在的城市)" 程序正常运行需要在同文件夹下加入一个 ...