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. The lesser known pitfalls of allowing file uploads on your website

    These days a lot of websites allow users to upload files, but many don’t know about the unknown pitf ...

  2. Spring-@value用法详解

    为了简化读取properties文件中的配置值,spring支持@value注解的方式来获取,这种方式大大简化了项目配置,提高业务中的灵活性. 一.两种使用方法 1.@Value("#{co ...

  3. 01-20Asp.net--Linq语法

    Linq语法--语言集成查询 同Sqlserver语句,但顺序颠倒了. 使用方法: 新建Linq类.dbml结尾的文件 在web窗体源代码中设计表,使用Repeater中转存放: <asp:Re ...

  4. python中的整数、浮点数和布尔值

    整数和浮点数有那个四则运算: 两种类型的数可以直接进行加减,当整数和浮点数进行加减的时候,结果会自动的变为浮点数,其中除法运算是“/”来表示的, 而余数的算术符号是“%”来表示的. 在布尔值的判断中我 ...

  5. 【总结整理】WebGIS基础

    1.万维网:www是world wide web的简称是在超文本基础上形成的信息网 2.互联网:即广域局域网及单机按照一定的通讯协议组成的国际计算机网络 3.WebGIS:网络地理信息系统,指基于In ...

  6. Codeforces 1108E (Array and Segments) 线段树

    题意:给你一个长度为n的序列和m组区间操作,每组区间操作可以把区间[l, r]中的数字都-1,请选择一些操作(可以都不选),使得序列的最大值和最小值的差值尽量的大. 思路:容易发现如果最大值和最小值都 ...

  7. 20、Basic Shell_for_while_grep_find

    转载:https://github.com/swcarpentry/DEPRECATED-boot-camps/blob/master/shell/shell_cheatsheet.md 1.For ...

  8. Luogu 1514 [NOIP2010] 引水入城

    我就是过来开心一下……这道题从开坑以来已经堆积了大半年了……今天才发现广搜一直写挂…… 丢个线段覆盖的模板,设$f_{i}$表示覆盖区间[1, i]的最小代价,$g_{i, j}$表示覆盖区间[i, ...

  9. 2018年第九届蓝桥杯国赛总结(JavaB组)

    懒更,之前的删了补一个国赛总结 记yzm10的第一次国赛(赛点:首都经贸大学) 第一次就拿到了国一,运气不要太好~(同组lz学长豪取国特orz) 从省赛一路水过来,总算有了点成绩.其实最后一题有些遗憾 ...

  10. html复杂url提交

    在使用手机向webapi发送照片请求的时候遇到一个问题,关于base64位字符串码无法识别,提交的表单是这样的: <form id="form1" method=" ...