http://www.elijahqi.win/2018/01/07/poj1390-blocks/
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
Liu Rujia@POJ
好难啊qwq icefox好强啊
这题我不看题解感觉根本想不出 膜Icefox巨佬也好久才看懂
首先我先预处理color数组表示把相同颜色的提前都合并起来
然后Len数组表示新的这一组的长度是多少 然后预处理的ava数组 代表我这个之后最多还能有几个连起来的
设dp[i][j][k]表示i~j区间 右端接长度为k的最高得分 当然这k个得和j的颜色相同才可以
dp[i][j][k]=dp[i][j−1][0]+(len[j]+k)2
或者还有一种可能我需要在i~j的区间内找到一个和j相同的 然后消去
dp[i][j][k]=max{dp[i][z][len[j]+k]+dp[z+1][j−1][0]}(z=i−>j−1)
然后转移一下即可
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 220
using namespace std;
inline char gc(){
static char now[1<<16],*S,*T;
if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0;char ch=gc();
while(ch<'0'||ch>'9') ch=gc();
while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();}return x;
}
int n,a[N],len[N],color[N],dp[N][N][N],ava[N];
int main(){
freopen("poj1390.in","r",stdin);
int T=read(),case1=0;
while(T--){
n=read();memset(len,0,sizeof(len));
for (int i=1;i<=n;++i) a[i]=read();int tot=0,cnt=0;ava[0]=n;
for (int i=1;i<=n;++i) if (a[i]!=color[tot]) len[tot]=cnt,color[++tot]=a[i],ava[tot]=ava[tot-1],
--ava[tot],cnt=1;else ++cnt,--ava[tot];len[tot]=cnt;memset(dp,0,sizeof(dp));
for (int i=tot;i>=1;--i){
for (int j=i;j<=tot;++j){
for (int k=0;k<=ava[j];++k){
dp[i][j][k]=dp[i][j-1][0]+(len[j]+k)*(len[j]+k);
for (int z=i;z<j;++z)
if (color[z]==color[j])dp[i][j][k]=max(dp[i][j][k],dp[i][z][len[j]+k]+dp[z+1][j-1][0]);
}
}
}
printf("Case %d: %d\n",++case1,dp[1][tot][0]);
}
return 0;
}