ACM: 动态规划 poj 1390

 
                                                                                      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:
ACM: <wbr>动态规划 <wbr>poj <wbr>1390
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:
ACM: <wbr>动态规划 <wbr>poj <wbr>1390
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

题意: n个带颜色的方格排成一列, 相同颜色的可以连成一个区域, 可以选取任意一个区域消去.
      如果区域里面有x个方格, 则你的得分是x^2. 要求消去全部的区域后的得分最大.
解题思路:
       1. 状态: dp[i][j][k]: 表示第i个区域到第j个区域,k表示i到j中与第j堆相同颜色的个数.
                     即: (color[i],len[i]),(color[i+1],len[i+1]),...,
                         (color[j-1],len[j-1]),(color[j],len[j]+k) 合并的最大得分.
       2. color[i], len[i]: 分别表示第i堆的颜色和第i堆的个数.
       3. 状态转移方程: dp[i][j][k] = max(
            dp[i][j-1][0]+(len[j]+k)^2, dp[i][p][k+len[j]] + dp[p+1][j-1][0] 
                                        );
            (i <= p < j)  满足的条件是当a[p] == a[j]时,因为第p个方块的颜色与第j个要一样.
 
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 205
int n;
int a[MAX];
int dp[MAX][MAX][MAX];
int len[MAX];
int num;
inline int max(int a,int b)
{
 return a > b ? a : b;
}
int solve(int i,int j,int k)
{
 int temp;
 if(dp[i][j][k] > -1)
 {
  return dp[i][j][k];
 }
 temp = solve(i,j-1,0)+(k+len[j])*(k+len[j]);
 for(int p = i; p < j; ++p)
 {
  if(a[p] == a[j])
  {
   temp = max( temp , solve(i,p,k+len[j])+solve(p+1,j-1,0) );
  }
 }
 dp[i][j][k] = temp;
 return temp;
}
int main()
{
 int i;
 int k = 1;
// freopen("input.txt","r",stdin);
 int caseNum;
 scanf("%d",&caseNum);
 while(caseNum--)
 {
  scanf("%d",&n);
  int temp;
  num = 0;
  for(i = 1; i <= n; ++i)
  {
   scanf("%d",&temp);
   if(temp == a[num])
    len[num]++;
   else
   {
    num++;
    a[num] = temp;
    len[num] = 1;
   }
  }
  memset(dp,-1,sizeof(dp));
  for(i = 1; i <= num; ++i)
   dp[i][i-1][0] = 0;
  int result = solve(1,num,0);
  printf("Case %d: %d\n",k++,result);
 }
 return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值