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. JDBC---bai

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import ...

  2. showModalDialog()子窗口刷新父窗口

    今天再次使用showModalDialog(),发现了两个问题,一是子窗口如何刷新父窗口,二是窗口的参数问题. 1 子窗口刷新父窗口 如果是window.open();问题就好办,直接用window. ...

  3. GridControl 隐藏Drag a column header here to group by that column

    解决方案: 打开设计器,找到OptionsView,往下拉设置showGroupPanel为false 

  4. Pandoc+markdown生成slides

    Pandoc+markdown生成slides 参考:http://blog.csdn.net/pizi0475/article/details/50955900 1.安装 http://pandoc ...

  5. CUDA编程接口:异步并发执行的概念和API

    1.主机和设备间异步执行 为了易于使用主机和设备间的异步执行,一些函数是异步的:在设备完全完成任务前,控制已经返回给主机线程了.它们是: 内核发射; 设备间数据拷贝函数; 主机和设备内拷贝小于64KB ...

  6. OpenCV 官方工程报错(1) Couldn't load mixed_sample from loader

    openCV/OpenCV-android-sdk/samples/tutorial-2-mixedprocessing 工程 - ::): Trying to get library list - ...

  7. [bzoj2460] [BeiJing2011]元素(线性基+贪心)

    题目大意: 有一些矿石,每个矿石有一个a和一个b值,要求选出一些矿石,b的和最大且不存在某个矿石子集它们的a的异或和为0. 解题关键:对魔力进行由大到小排序,依次加入线性基,统计即可. #includ ...

  8. [patl2-001]紧急救援

    解题关键:最短路的变形. 1.按顶点存储,$O(n^2)$ #include<cstdio> #include<cstring> #include<algorithm&g ...

  9. 框架之Struts2

    相比较hibernate简单了许多 案例:使用Struts2框架完成登录功能 需求分析 1. 使用Struts2完成登录的功能 技术分析之Struts2框架的概述 1. 什么是Struts2的框架 * ...

  10. 1027C Minimum Value Rectangle

    传送门 题目大意 有n个木棍,让你选4根使得组成的矩形的周长的平方除以面积最小. 分析 这个题看起来就是一个需要证明的贪心,下面我们来证明一下: 所以我们只需要枚举一边所有的a的可能值,然后b就是比a ...