Blocks
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 3796 | Accepted: 1506 |
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.
The corresponding picture will be as shown below:
![](https://i-blog.csdnimg.cn/blog_migrate/5c0acd096a405d420783e24c6481dd83.jpeg)
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:
![](https://i-blog.csdnimg.cn/blog_migrate/2a36e1f57f0e2d975e7747be82780c41.jpeg)
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
题解:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 205
int n, color[MAXN], len[MAXN];
int preIndex[MAXN], afterSum[MAXN];
int dp[MAXN][MAXN][MAXN];
void show()
{
for(int i = 1; i <= n; i++)
printf("color[%d] = %d, len[%d] = %d\n", i, color[i], i, len[i]);
for(int i = 1; i <= n; i++)
printf("afterSum[%d]= %d, preIndex[%d] = %d\n", i, afterSum[i], i, preIndex[i]);
}
int operDP()
{
int l, i, j, k, p;
memset(dp, 0, sizeof(dp));
for(l = 1; l <= n; l++)
{
for(i = 1; i + l - 1 <= n; i++)
{
j = i + l - 1;
for(k = 0; k <= afterSum[j]; k++)
{
dp[i][j][k] = dp[i][j-1][0] + (len[j] + k) * (len[j] + k);
for(p = preIndex[j]; p >= i; p = preIndex[p])
dp[i][j][k] = max(dp[i][j][k], dp[i][p][k+len[j]] + dp[p+1][j-1][0]);
}
}
}
return dp[1][n][0];
}
int main()
{
int T;
scanf("%d", &T);
for(int cs = 1; cs <= T; cs++)
{
scanf("%d", &n);
int i, j = 0, clr = 0;
int a[MAXN] = {-1, 0}, vis[MAXN] = {0};
for(i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
if(a[i] == a[i-1])
len[j]++;
else
{
if(vis[a[i]] == 0)
vis[a[i]] = ++clr;
color[++j] = vis[a[i]];
len[j] = 1;
}
}
n = j;
memset(preIndex, -1, sizeof(preIndex));
for(i = n; i >= 1; i--)
{
for(j = i - 1; j >= 1; j--)
if(color[i] == color[j]) break;
if(j >= 1) preIndex[i] = j;
}
memset(afterSum, 0, sizeof(afterSum));
for(i = n; i >= 1; i--)
{
for(j = i + 1; j <= n; j++)
if(color[i] == color[j])
afterSum[i] += len[j];
}
//show();
int ans = operDP();
printf("Case %d: %d\n", cs, ans);
}
return 0;
}