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. 读入输出优化_C++

    当我们考试时遇到大量的读入或者输出时,这些代码会耗费许多运行程序的时间,导致TL 本来 log2n 的算法因为读入被卡成线性的就太不划算了,所以我们这里要采用读入输出优化 getchar 和 putc ...

  2. Java工厂模式浅析理解

    由于本人缺乏工作经验,本篇文章作为随笔,只是对工厂模式有一个简单的认识 工厂模式分为以下三种: 1:简单工厂(Simple Factory).2:工厂方法(Factory Method).3:抽象工厂 ...

  3. windows下xampp安装PHP的pthreads多线程扩展

    我的运行环境: 系统:windows10 ,64位 PHP:5.6.8 TS,VC11 ,32位 Apache: 2.0 我安装的是xampp集成环境 pthreads的windows扩展文件下载地址 ...

  4. zabbix报警邮件qq邮箱收不到的问题

    出现这样问题的根本原因是因为zabbix对中文支持不太友好,乱码导致了邮件无法正常接收. 一.解决zabbix邮件内容为附件 1.安装发送邮件的mailx 以及windows文件转unix文件的命令 ...

  5. cloudflare 301 重定向设置

    https://support.cloudflare.com/hc/en-us/articles/218411427#redirects 将 https://dfg.com/* 设置301重定向到 h ...

  6. MVC中Model和model的区别和用户

    MVC中Model和model的区别,它们应该怎么用呢? 使用@model关键字可以定义一个Action里所对应的一个模型(经常可以叫他实体类). MVC的第一个字母M是Model,承载着View层和 ...

  7. 快速幂取模(当数很大时,相乘long long也会超出的解决办法)

    当几个数连续乘最后取模时,可以将每个数字先取模,最后再取模,即%对于*具有结合律.但是如果当用来取模的数本身就很大,采取上述方法就不行了.这个时候可以借鉴快速幂取模的方法,来达到大数相乘取模的效果. ...

  8. 洛谷 P1064 金明的预算方案【有依赖的分组背包】

    题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:"你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过N元钱 ...

  9. 洛谷——P2660 zzc 种田

    P2660 zzc 种田 题目背景 可能以后 zzc就去种田了. 题目描述 田地是一个巨大的矩形,然而zzc 每次只能种一个正方形,而每种一个正方形时zzc所花的体力值是正方形的周长,种过的田不可以再 ...

  10. [POI2014]Supercomputer

    题目大意: 给定一个$n(n\le10^6)$个结点的有根树,从根结点开始染色.每次可以染和已染色结点相邻的任意$k$个结点.$q(q\le10^6)$组询问,每次给定$k$,问至少需要染几次? 思路 ...