Blocks
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 4318   Accepted: 1745

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

递归形式的动态规划:dp[st][ed][len]从st到ed全然消除。且ed右边挨着有一个len的大块颜色和ed同样.

一种消除方式是,Len块直接和ed块合并直接消除得到分数work(st,ed-1,0)+(a[ed].n+len)*(a[ed].n+len);

还有一种是在st到ed之间找到一个块p和ed块颜色同样,把这3块直接合并 work(st,p,a[ed].n+len)+work(p+1,ed-1,0);

两种方式取最大的值。

当st==ed时递归结束。

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<queue>
using namespace std;
#define LL __int64
#define N 210
const int inf=0x1f1f1f1f;
struct node
{
int c,n,p;
}a[N];
int f[N][N][N];
int work(int st,int ed,int len)
{
if(f[st][ed][len])
return f[st][ed][len];
int i,ans=(a[ed].n+len)*(a[ed].n+len);
if(st==ed)
{
f[st][ed][len]=ans;
return ans;
}
ans+=work(st,ed-1,0);
for(i=ed-1;i>=st;i--)
{
if(a[i].c!=a[ed].c)
continue;
int tmp=work(st,i,a[ed].n+len)+work(i+1,ed-1,0);
if(tmp<=ans)
continue;
ans=tmp;
break;
}
f[st][ed][len]=ans;
return ans;
} int main()
{
int T,t,cnt,i,n,Cas=1;
scanf("%d",&T);
while(T--)
{
memset(a,0,sizeof(a));
scanf("%d",&n);
scanf("%d",&t);
cnt=0;
a[cnt].c=t;
a[cnt].n=1;
for(i=1;i<n;i++)
{
scanf("%d",&t);
if(t==a[cnt].c)
{
a[cnt].n++;
}
else
{
cnt++;
a[cnt].c=t;
a[cnt].n=1;
}
}
memset(f,0,sizeof(f));
printf("Case %d: %d\n",Cas++,work(0,cnt,0));
}
return 0;
}

poj 1390 Blocks (记忆化搜索)的更多相关文章

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

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

  2. POJ 1088 DP=记忆化搜索

    话说DP=记忆化搜索这句话真不是虚的. 面对这道题目,题意很简单,但是DP的时候,方向分为四个,这个时候用递推就好难写了,你很难得到当前状态的前一个真实状态,这个时候记忆化搜索就派上用场啦! 通过对四 ...

  3. POJ 1088 滑雪 (记忆化搜索)

    题目链接:http://poj.org/problem?id=1088 题意很好懂,就是让你求一个最长下降路线的长度. dp[i][j]记录的是i j这个位置的最优的长度,然后转移方程是dp[i][j ...

  4. POJ 1088 滑雪(记忆化搜索+dp)

    POJ 1088 滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 107319   Accepted: 40893 De ...

  5. 专题1:记忆化搜索/DAG问题/基础动态规划

      A OpenJ_Bailian 1088 滑雪     B OpenJ_Bailian 1579 Function Run Fun     C HDU 1078 FatMouse and Chee ...

  6. POJ 1088 滑雪(记忆化搜索)

    滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 92384   Accepted: 34948 Description ...

  7. poj 3249 Test for Job (DAG最长路 记忆化搜索解决)

    Test for Job Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 8990   Accepted: 2004 Desc ...

  8. (区间dp + 记忆化搜索)Treats for the Cows (POJ 3186)

    http://poj.org/problem?id=3186   Description FJ has purchased N (1 <= N <= 2000) yummy treats ...

  9. poj 3249(bfs+dp或者记忆化搜索)

    题目链接:http://poj.org/problem?id=3249 思路:dp[i]表示到点i的最大收益,初始化为-inf,然后从入度为0点开始bfs就可以了,一开始一直TLE,然后优化了好久才4 ...

随机推荐

  1. Codewars练习Python

    计算一个数组的中间数,数的两边和相等,并返回index值 如:数组[1,2,3,4,6] 返回3(数组序号从0开始) def find_even_index(arr): ""&qu ...

  2. Selenium常用方法及函数

    新建实例driver = webdriver.Chrome() 1.获取当前页面Url的函数方法:current_url实例:driver.current_url 2.表单的提交方法:submit解释 ...

  3. mfc按钮悬停显示文字

    .h CToolTipCtrl m_toopTip; .cpp oninitdialog void CDlgDwgLibMan::InitTooltips(){ EnableToolTips(); m ...

  4. SpringBoot项目编译后没有xxxmapper.xml文件解决方法

    在pom.xml文件中添加如下代码 <build> <plugins> <plugin> <groupId>org.springframework.bo ...

  5. 03Servlet API

    Servlet API Servlet是实现javax.servlet.Servlet接口的对象.大多数Servlet通过从GenericServlet或HttpServlet类进行扩展来实现.Ser ...

  6. Python学习之LeetCode刷题之路——简单题【1、7、9】

    1.两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个 ...

  7. linux top-显示或管理执行中的程序

    推荐:更多linux 性能监测与优化 关注:linux命令大全 top命令可以实时动态地查看系统的整体运行情况,是一个综合了多方信息监测系统性能和运行信息的实用工具.通过top命令所提供的互动式界面, ...

  8. ubuntu root用户登陆

    sudo vi /etc/lightdm/lightdm.conf   (如果没有该文件则创建,内容如下) [SeatDefaults] user-session=ubuntu greeter-ses ...

  9. 虚拟机下Linux网络配置

    之前配置好了linux系统,在网络这块我用的是桥接模式. 现在分享一下使用虚拟机桥接模式配置Linux网络的过程. 一.首先配置外网的本地Ip地址. 二.配置Linux 网络链接 1.打开linux网 ...

  10. 基于python、jupyter-notebook 的金融领域用户交易行为分析

    说明:本文重在说明交易数据统计.分析方法,所有数据均为生成的数据 时间原因代码未定义成函数 统计指标:1.用户单日交易行为数据 2.按小时为计算单位,统计用户行为数据(旨在求得一天24小时中每个小时的 ...