Blocks

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 5252   Accepted: 2165

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

Solution

lrj出的题好劲啊......写的TA爷的做法

一个比较厉害的状态$f[l][r][k]$表示当$a[l]==a[r]$时,将$l~r$这个整段玩到还剩连续的$k$个$a[l]/a[r]$色的块时得到的最大的值。

但是单靠这个是不足以转移的,

另一个状态$g[l][r]$表示,不管以什么方法,删光$l~r$这段的得到的最大的值。

然后进行区间DP,显然要枚举区间,枚举断点,转移就是;

$$f[l][r][k]=max(f[l][r][k],f[l][r][k-1]+g[k'-1][r-1]);$$

这个转移就是把区间$l~r$中花式删掉$k'~r-1$这段,剩下的组成$k$个的最大方案。

$$g[l][r]=max(g[l][r],f[l][r][k]+k*k,g[l][k']+g[k'+1][r]);$$

这个转移比较显然..

然后这样转移之后,显然$f[l][r][0]=g[l][r]$.

答案就是$f[1][N][0]/g[1][N]$,时间复杂度是$<=O(N^4)$的,而且在正常的数据下表现非常优秀。

Code

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
inline int read()
{
int x=,f=; char ch=getchar();
while (ch<'' || ch>'') {if (ch=='-') f=-; ch=getchar();}
while (ch>='' && ch<='') {x=x*+ch-''; ch=getchar();}
return x*f;
}
int T,N,a[],f[][][],g[][],t,cnt[][];
int main()
{
T=read();
while (T--)
{
N=read();
for (int i=; i<=N; i++) a[i]=read();
memset(cnt,,sizeof(cnt));
for (int i=; i<=N; i++)
for (int j=i+; j<=N; j++)
if (a[i]==a[j])
for (int k=i; k<=j; k++)
if (a[k]==a[i]) cnt[i][j]++;
memset(f,,sizeof(f)); memset(g,,sizeof(g));
for (int i=; i<=N; i++) g[i][i]=f[i][i][]=,f[i][i][]=;
for (int len=; len<=N; len++)
for (int l=; l+len-<=N; l++)
{
int r=l+len-;
for (int i=; i<=cnt[l][r]; i++)
{
for (int j=l; j<r; j++)
f[l][r][i]=max(f[l][r][i],f[l][j][i-]+g[j+][r-]);
g[l][r]=max(g[l][r],f[l][r][i]+i*i);
}
for (int i=l; i<r; i++) g[l][r]=max(g[l][r],g[l][i]+g[i+][r]);
f[l][r][]=g[l][r];
}
printf("Case %d: %d\n",++t,f[][N][]);
}
return ;
}

【POJ-1390】Blocks 区间DP的更多相关文章

  1. POJ 1390 Blocks(区间DP)

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

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

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

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

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

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

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

  5. POJ1390 Blocks (区间DP)

    题目链接:POJ 1390.Blocks 题意: 有n个方块排成一列,每个方块有颜色即1到n的一个值,每次操作可以把一段相同颜色的方块拿走,长度为k,则获得的分数为 \(k\times k\),求可获 ...

  6. poj 1390 Blocks

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

  7. POJ 2995 Brackets 区间DP

    POJ 2995 Brackets 区间DP 题意 大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配.需要注意的是这里的匹配规则. 解题思路 区间DP,开始自己没想到是区间 ...

  8. UVA10559&POJ1390 Blocks 区间DP

    题目传送门:http://poj.org/problem?id=1390 题意:给出一个长为$N$的串,可以每次消除颜色相同的一段并获得其长度平方的分数,求最大分数.数据组数$\leq 15$,$N ...

  9. POJ 1179 - Polygon - [区间DP]

    题目链接:http://poj.org/problem?id=1179 Time Limit: 1000MS Memory Limit: 10000K Description Polygon is a ...

随机推荐

  1. wamp集成环境开启rewrite伪静态支持

    wamp集成环境在安装完后,默认是没有开启伪静态的,所以有时把项目部署进去时如果源码里包含.htaccess文件的话,可能会出现500错误,这一般是因为不支持伪静态造成的,解决这个问题的办法就是开启伪 ...

  2. js通过循环多张图片实现动画效果

    以小鱼摇尾巴和眨眼睛为例 动画思路: 1.将图片资源放在数组里面 2.通过计时器来设定间隔时间 3.通过计数器来取相应的图片 第一步:基本框架,鱼身体 <body> <canvas ...

  3. Linux2.6内核进程调度系列--scheduler_tick()函数3.更新普通进程的时间片

    RT /** * 运行到此,说明进程是普通进程.现在开始更新普通进程的时间片. */ /* 首先递减普通进程的时间片计数器.如果用完,继续执行以下操作 */ if (!--p->time_sli ...

  4. android清除本应用里的各种数据的方法

    public class DataCleanManager { /** * * 清除本应用内部缓存(/data/data/com.xxx.xxx/cache) * * * * @param conte ...

  5. servlet应用及知识点总结

    1. servlet的web.xml中的配置 ------------------------------------------------------------------1. response ...

  6. WinSetupFromUSB - 制作多系统U盘安装All-In-One的利器

    单U盘玩多个操作系统,这下有福了! 官网最新的下载地址:http://www.winsetupfromusb.com/files/download-info/winsetupfromusb-1-7-e ...

  7. NFS网络共享服务部署

    10.3 NFS服务端部署环境准备 10.3.1 NFS服务部署服务器准备 服务器系统 角色 IP Centos6.7 x86_64 NFS服务器端(NFS-server) 192.168.1.14 ...

  8. 基于easyUI实现组织结构树图形

    一. 准备工作 1. 点击此下载相关文件 2. 进入 js 文件夹,解压缩 jquery-easyui-1.5.rar 到当前文件夹 二. 在浏览器中运行 organize.html 文件,即可看到效 ...

  9. 前端HTML之页面结构

    前端工作一年了,期间由于工作需要,也做了一些产品的设计,因为自己的目标就是做编程,所以婉拒了与产品相关的一些任务,打算主要把精力放到编程这方面. PS:2015年1月进军编程行业. 废话不多讲,这一年 ...

  10. java中的浮点数

    浮点数值不适用于禁止出现舍入误差的金融计算中.例如,命令System.out.println(2.0-1.1)将打印出0.8999999999999999999999999,而不是人们想象的0.9.其 ...