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. glibc2.12升级至2.15

    1.操作系统版本 [root@localhost ~]# cat /etc/redhat-release #CentOS release 6.9 (Final) 2.当前glibc版本 [root@l ...

  2. Yii2实现跨mysql数据库关联查询排序功能

    遇到一个项目,需要跨表网上找了很多的资料,整理一下,方便以后再次使用 背景:在一个mysql服务器上(注意:两个数据库必须在同一个mysql服务器上)有两个数据库: memory (存储常规数据表) ...

  3. thinkPHP5.0 save和saveAll,新增和更新的问题

    今天遇到一个问题,在模型中使用save保存数据之后,使用saveAll继续新增数据,结果报 缺少更新条件,网上搜了下发现一篇文章https://www.jianshu.com/p/1848f61de6 ...

  4. strak组件(9):关键字搜索

    效果图: 在列表视图函数增加搜索功能. 新增函数 def get_search_list(self) 钩子方法,获取搜索条件 一.strak组件 strak/service/core_func.py ...

  5. Codeforces Round #428 (Div. 2) C. dfs

    C. Journey time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  6. [BZOJ1060][ZJOI2007]时态同步(树形DP)

    [我是传送门] 因为边权只能增加,那么设f[u]为u子树上从i出发到达某个叶节点的最大路径, 显然Ans应该增加f[u]-f[v]-e[i].w Code #include <cstdio> ...

  7. <s:submit> 指定的method方法不执行

    很多文章在讲一个表单多个提交方法的时候都是在<s:submit>中通过method来指定,但是我在试验中怎么也不对,jsp页面代码如下 <%@page import="or ...

  8. Android面试收集录10 LruCache原理解析

    一.Android中的缓存策略 一般来说,缓存策略主要包含缓存的添加.获取和删除这三类操作.如何添加和获取缓存这个比较好理解,那么为什么还要删除缓存呢?这是因为不管是内存缓存还是硬盘缓存,它们的缓存大 ...

  9. Android 自定义圆形图片 CircleImageView

    1.效果预览 1.1.布局中写自定义圆形图片的路径即可 1.2.然后看一看图片效果 1.3.原图是这样的 @mipmap/ic_launcher 2.使用过程 2.1.CircleImageView源 ...

  10. Spring---配置文件概述

    概述 Spring 的配置文件是用于指导 Spring 工厂进行Bean的生产.依赖关系注入及 Bean 实例分发的“图纸”,它是一个或多个标准的XML文档,J2EE 程序员必须学会并灵活应用这份“图 ...