Blocks
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 5035   Accepted: 2065

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

 
题意:N个方盒(box)摆成一排,每个方盒有自己的颜色。连续摆放的同颜色方盒构成 一个方盒片段(box segment)玩家每次点击一个方盒,则该方盒所在方盒片段就会消失。若消失的方盒片段 中共有k个方盒,则玩家获得k*k个积分 求获得的最高积分
 
题解:参考pku_gw代码   dp姿势涨
score[i][j][len] 表示在i~j的方盒片段上 片段j右侧有长度为len的且与j颜色相同的片段 获得的最高分
但是左侧的和j颜色相同的怎么处理呢  就需要枚举左侧的片段,判断分块处理j片段更优还是合并处理
更优  递归的终止条件问i==j
 
 /******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#define ll long long
#define mod 1000000007
#define PI acos(-1.0)
using namespace std;
int t;
int n;
struct node
{
int color;
int len;
}se[];
int score[][][];
int fun(int l,int r,int len)
{
if(score[l][r][len]>)
return score[l][r][len];
int re=(se[r].len+len);//直接处理右侧
re=re*re;
if(l==r)
{
score[l][r][len]=re;
return score[l][r][len];
}
re+=fun(l,r-,);
for(int j=r-;j>=l;j--)//枚举左侧片段
{
if(se[j].color!=se[r].color) continue;
int temp=fun(l,j,se[r].len+len)+fun(j+,r-,);//递归 分解 将右侧的合并到左侧
if(temp<=re) continue;//判断那个更优
re=temp;
break;
}
score[l][r][len]=re;
return score[l][r][len];
}
int main()
{
scanf("%d",&t);
int coun=;
int flag=;
memset(se,,sizeof(se));
while(t--){
int coun=;
int exm;
scanf("%d",&n);
scanf("%d",&se[coun].color);
se[coun].len=;
for(int i=;i<n;i++)//分解片段的过程
{
scanf("%d",&exm);
if(exm==se[coun].color)
se[coun].len++;
else
{
coun++;
se[coun].color=exm;
se[coun].len=;
}
}
memset(score,,sizeof(score));
printf("Case %d: %d\n",flag++,fun(,coun,));
}
return ;
}
 

poj 1390 区间dp的更多相关文章

  1. poj 3280(区间DP)

    Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7869   Accepted: 38 ...

  2. poj 2955 区间dp入门题

    第一道自己做出来的区间dp题,兴奋ing,虽然说这题并不难. 从后向前考虑: 状态转移方程:dp[i][j]=dp[i+1][j](i<=j<len); dp[i][j]=Max(dp[i ...

  3. POJ 2955 (区间DP)

    题目链接: http://poj.org/problem?id=2955 题目大意:括号匹配.对称的括号匹配数量+2.问最大匹配数. 解题思路: 看起来像个区间问题. DP边界:无.区间间隔为0时,默 ...

  4. POJ 1651 (区间DP)

    题目链接: http://poj.org/problem?id=1651 题目大意:加分取牌.如果一张牌左右有牌则可以取出,分数为左牌*中牌*右牌.这样最后肯定还剩2张牌.求一个取牌顺序,使得加分最少 ...

  5. POJ 1141 区间DP

    给一组小括号与中括号的序列,加入最少的字符,使该序列变为合法序列,输出该合法序列. dp[a][b]记录a-b区间内的最小值, mark[a][b]记录该区间的最小值怎样得到. #include &q ...

  6. POJ 1651 区间DP Multiplication Puzzle

    此题可以转化为最优矩阵链乘的形式,d(i, j)表示区间[i, j]所能得到的最小权值. 枚举最后一个拿走的数a[k],状态转移方程为d(i, j) = min{ d(i, k) + d(k, j) ...

  7. poj 1141 区间dp+递归打印路径

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30383   Accepted: 871 ...

  8. POJ 3042 区间DP(费用提前计算相关的DP)

    题意: 思路: f[i][j][1]表示从i到j的区间全都吃完了 现在在j点 变质期最小是多少 f[i][j][0]表示从i到j的区间全都吃完了 现在在i点 变质期最小是多少 f[i][j][0]=m ...

  9. POJ 2955 区间DP必看的括号匹配问题,经典例题

    Brackets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14226 Accepted: 7476 Description ...

随机推荐

  1. ethereum(以太坊)(十二)--应用(二)__投票(基础总和)

    编写应用合约之前,先弄清它的逻辑,有助于我们更好的部署合约 pragma solidity ^0.4.21; pragma experimental ABIEncoderV2; contract vo ...

  2. python之微信自动发送消息

    代码如下: from __future__ import unicode_literals from threading import Timer from wxpy import * import ...

  3. 学习Pytbon第十七篇,面向对象编程

    面向对象技术简介 类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 类变量:类变量在整个实例化的对象中是公用的.类变量定义在类 ...

  4. caioj:1348: [NOIP普及组2012]质因数分解 C++

    题目描述 已知正整数n是两个不同的质数的乘积,试求出两者中较大的那个质数. 输入样例 21 输出样例 7 输入 输入只有一行,包含一个正整数n. 输出 输出只有一行,包含一个正整数p,即较大的那个质数 ...

  5. [Bzoj2246]迷宫探险(概率+DP)

    Description 题目链接 Solution 用三进制表示陷阱状态,1表示有害,2表示无害,0表示不知道 用\(f[S][i]\)表示状态为S时陷阱i有害的概率,这个可以预处理出 \(d[S][ ...

  6. 笔记-python -asynio

    笔记-python -asynio 1.      简介 asyncio是做什么的? asyncio is a library to write concurrent code using the a ...

  7. android singleTop 不起作用

    今天,排查问题,发现设置了singleTop 的activity, 多次启动依然是多个acitivity,而不是一个. 明明在清单里面设置了,但是就是启动了多个. 可能是因为启动的太快,导致系统判断有 ...

  8. Kafka实践、升级和新版本(0.10)特性预研

    本文来自于网易云社区 一.消息总线MQ和Kafka (挡在请求的第一线) 1. 几个应用场景 case a:上游系统往下游系统推送消息,而不关心处理结果: case b:一份新数据生成,需要实时保存到 ...

  9. 图的最短路径:Dijkstra 和 Floyd

    //最短路径 /* dijkstra Dijkstra(迪杰斯特拉)算法的核心思想是贪心策略+动态规划 http://www.programgo.com/article/4721147659/ Dij ...

  10. Jforum环境之Tomcat环境搭建

    Jforum环境搭建,需先安装JDK.JRE.Tomcat.Mysql(JDK.JRE暂不做说明).本文先说Tomcat环境搭建 1.进入Apache Tomcat官网下载,我选择的是免安装的zip包 ...