Blocks
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 1916 Accepted: 734
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 anexample: Gold, Silver, Silver, Silver, Silver, Bronze, Bronze, Bronze, Gold.
The corresponding picture will be as shownbelow:
Figure 1
If some adjacent boxes are all of the samecolor, 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 thesegments respectively.
Every time, you can click a box, then thewhole segment containing that box DISAPPEARS. If that segment is composed of kboxes, you will get k*k points. for example, if you click on a silver box, thesilver 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, givenan initial state of this game.
Input
The first line contains the number of testst(1<=t<=15). Each case contains two lines. The first line contains aninteger n(1<=n<=200), the number of boxes. The second line contains nintegers, representing the colors of each box. The integers are in the range1~n.
Output
For each test case, print the case numberand 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
详见刘汝佳的黑书。。我好像隐隐约约知道什么叫记忆化搜索了。。。。。。
Source Code
Problem: 1390 User: creamxcream
Memory: 36416K Time: 1579MS
Language: C++ Result: Accepted
Source Code
#include <cstdio>
#include <cstring>
#define MAXN 210
#define INF -1
int color[MAXN],len[MAXN];
int n,num;
int f[MAXN][MAXN][MAXN];
void init()
{
memset(f,-1,sizeof(f));
scanf("%d",&n);
int temp;
scanf("%d",&temp);
color[1]=temp;
len[1]=1;
num=1;
for(int i=2;i<=n;i++)
{
scanf("%d",&temp);
if (temp==color[num])
len[num]++;
else
{
num++;
color[num]=temp;
len[num]=1;
}
}
}
int dpit(int i,int j,int k)
{
if (f[i][j][k]!=-1)
return f[i][j][k];
if (i==j+1) return 0;
int ans=INF;
int temp=dpit(i,j-1,0)+(len[j]+k)*(len[j]+k);
if (temp>ans)
ans=temp;
for(int p=i;p<j;p++)
if (color[p]==color[j])
{
temp=dpit(i,p,len[j]+k)+dpit(p+1,j-1,0);
if (temp>ans)
ans=temp;
}
f[i][j][k]=ans;
return ans;
}
int main()
{
int t;
scanf("%d",&t);
for (int i=1;i<=t;i++)
{
init();
int ans=dpit(1,num,0);
printf("Case %d: %d\n",i,ans);
}
return 0;
}