poj 1390 Blocks 【区间dp】

Blocks
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 4747 Accepted: 1932

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


题意:给定n个连续的块,每次可以把x个相同颜色的连续的块消去,获得价值x * x,问消去所有块可以获得的最大价值。


思路:对一个区间[x, y],每次分割区间再合并一起显然不是最优的。

如 1 2 3 2 1 2 3 2,最优方案肯定是把4个2一块消去,因此不能单纯考虑每个区间。

因为消去一个块i,我们必须要考虑到其它与它颜色相同的块,这样,我们只考虑前面的块好了(后面的一样)。

设置dp[i][j][k]的状态。

k表示在i前面有k个与i颜色相同的块,这样dp[i][j][k]表示消去前面k个与i颜色相同的块 + 区间[i, j]里面的块 可以获得的最大价值。

考虑消去前面k个与i颜色相同的块,可以直接消去,也可以在区间[i, j]里面某个与i颜色相同的块一起消去。

转移(i < l <= j && a[l] == a[i])

dp[i][j][k] = max(dp[i+1][j][k] + (1 + k) * (1 + k), dp[l][j][k+1] + dp[i+1][l-1][0]);


AC代码:


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <string>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (200+10)
#define MAXM (500000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
using namespace std;
int a[MAXN];
int dp[MAXN][MAXN][MAXN];
int DFS(int i, int j, int k)
{
    if(dp[i][j][k] != -1) return dp[i][j][k];
    if(i > j) return dp[i][j][k] = 0;
    dp[i][j][k] = DFS(i+1, j, 0) + (1 + k) * (1 + k);
    for(int l = i+1; l <= j; l++)
        if(a[l] == a[i])
            dp[i][j][k] = max(dp[i][j][k], DFS(l, j, k+1) + DFS(i+1, l-1, 0));
    //dp[i][j] = max(dp[i][j], DFS(i, k) + DFS(k+1, j));
    return dp[i][j][k];
}
int main()
{
    int t, kcase = 1; Ri(t);
    W(t)
    {
        int n; Ri(n);
        for(int i = 1; i <= n; i++)
            Ri(a[i]);
        CLR(dp, -1);
        printf("Case %d: %d\n", kcase++, DFS(1, n, 0));
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值