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;
设dp[ i ][ j ][ k ]表示i~j区间,最后合并k个的最大值;
dp[ i ][ j ][ k ]=dp[ i ][ j-1 ][ 0 ]+( len[ j ]+k )^2;
第二种情况就是中间一段先消去,然后与后面那一段拼接消除;
dp[ i ][ j ][ k ]=dp[ i ][ k ][ len[ j ]+k ]+dp[ k+1 ][ j-1 ][ 0 ];
那么我们记忆化dfs即可;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 400005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ ll mode;
struct matrix {
ll n, m, a[10][10];
matrix(ll n, ll m) {
this->n = n; this->m = m; ms(a);
}
matrix(ll n, ll m, char c) {
this->n = n; this->m = m; ms(a);
for (int i = 1; i <= n; i++)a[i][i] = 1;
}
ll *operator [](const ll x) {
return a[x];
}
matrix operator *(matrix b) {
matrix c(n, b.m);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= b.m; j++) {
for (int k = 1; k <= m; k++) {
c[i][j] = (c[i][j] + a[i][k] % mode*b[k][j] % mode) % mode;
}
}
}
return c;
}
void operator *=(matrix &b) {
*this = *this *b;
}
matrix operator ^(ll b) {
matrix ans(n, m, 'e'), a = *this;
while (b) {
if (b % 2)ans = ans * a; a *= a; b >>= 1;
}
return ans;
}
}; int dp[202][202][202];
int T;
int n;
int col[210];
int len[202];
int fg;
int dfs(int x, int y, int k) {
if (dp[x][y][k])return dp[x][y][k];
if (x == y)return (len[x] + k)*(len[x] + k);
dp[x][y][k] = dfs(x, y - 1, 0) + (len[y] + k)*(len[y] + k);
for (int i = x; i < y; i++) {
if (col[i] == col[y]) {
dp[x][y][k] = max(dp[x][y][k], dfs(x, i, len[y] + k) + dfs(i + 1, y - 1, 0));
}
}
return dp[x][y][k];
} int main()
{
//ios::sync_with_stdio(0);
rdint(T); int cnt = 0;
while (T--) {
cnt++;
ms(dp); ms(col); ms(len);
fg = 0;
int ans = 0;
rdint(n);
for (int i = 1; i <= n; i++) {
int tmp; rdint(tmp);
if (col[fg] == tmp)len[fg]++;
else fg++, len[fg] = 1, col[fg] = tmp;
}
ans = dfs(1, fg, 0);
cout << "Case " << cnt << ": " << ans << endl;
}
return 0;
}

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

  1. Blocks题解(区间dp)

    Blocks题解 区间dp 阅读体验...https://zybuluo.com/Junlier/note/1289712 很好的一道区间dp的题目(别问我怎么想到的) dp状态 其实这个题最难的地方 ...

  2. 【Uva10559】Blocks(区间DP)

    Description 题意:有一排数量为N的方块,每次可以把连续的相同颜色的区间消除,得到分数为区间长度的平方,然后左右两边连在一起,问最大分数为多少. \(1\leq N\leq200\) Sol ...

  3. UVA10559 方块消除 Blocks(区间dp)

    一道区间dp好题,在GZY的ppt里,同时在洛谷题解里看见了Itst orz. 题目大意 有n个带有颜色的方块,没消除一段长度为 \(x\) 的连续的相同颜色的方块可以得到 \(x^2\) 的分数,用 ...

  4. POJ 1390 Blocks(区间DP)

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

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

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

  6. 【POJ-1390】Blocks 区间DP

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

  7. POJ1390 Blocks (区间DP)

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

  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. 01-19asp.net网站--关于“应用程序中的服务器错误(需添加"Jquery"ScriptRescourseMapping)”

    一般打开网页进行加载时(有缓存),会弹出以下对话框. 但是如果网页加载后出现以下错误,就是应用程序的问题了.如果出现这种问题,就需要在安装Csharp的根目录下,找到一个名为.dll结尾的Jquery ...

  2. openGL 预定义变量04

    OpenGL4.0 GLSL预定义变量 GLSL为不同的渲染阶段定义了一些特定的变量.这些预定义(也叫做内置变量)有特定的属性. 所有的预定义变量都以gl_开头.用户定义的变量不能以此开头. 下面分类 ...

  3. css的relative与absolute(一)

    relative与absolute是position的两个值,本文对这两个值得关系进行了一个小实验 实验一: 首先定义了两个div元素,代码如下所示: <!doctype html> &l ...

  4. apache server和tomcat集群配置二:垂直负载

    垂直负载就是同一个机器中的不同服务器之间的负载.跟水平负载(ip不一样的服务器之间的负载)的最大区别就是要修改tomcat的端口号,避免引起冲突. 还要注意apache中workers.propert ...

  5. apache2不识别php

    sudo apt-get install libapache2-mod-php7.0 sudo a2enmod php7.0 sudo service apache2 restart 注意:Apach ...

  6. How to watch property in attrs of directive

    Q: I have a controller that has a counter that changes from time to time. That counter is tied to an ...

  7. 设置MySQL允许外网访问(转)

    设置MySQL允许外网访问   1.修改配置文件sudo vim /etc/mysql/my.cnf把bind-address参数的值改成你的内/外网IP或0.0.0.0,或者直接注释掉这行. 2.登 ...

  8. 容器控件JPanel的使用

    -----------------siwuxie095 工程名:TestUI 包名:com.siwuxie095.ui 类名:TestPanel.java 工程结构目录如下: 在默认窗体 JFrame ...

  9. 阿里云、宝塔、wordpress建站

    1 阿里云 购买一个学生机就行啦 2 宝塔 2.1 更改阿里云的镜像 技巧01:先关掉阿里云之前的镜像 技巧02:到镜像市场中寻找宝塔的镜像资源 2.2 配置安全组 宝塔的控制面板需要开通端口 888 ...

  10. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-003比较算法及算法的可视化

    一.介绍 1. 2. 二.代码 1. package algorithms.elementary21; /*********************************************** ...