【POJ-1390】Blocks 区间DP

本文介绍了一种求解游戏Blocks最高得分的算法。通过定义两个状态f和g,利用区间动态规划实现最优解的计算。最终输出对于给定游戏初始状态所能获得的最高分数。

Blocks

Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 5252 Accepted: 2165

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

Solution

lrj出的题好劲啊......写的TA爷的做法

一个比较厉害的状态$f[l][r][k]$表示当$a[l]==a[r]$时,将$l~r$这个整段玩到还剩连续的$k$个$a[l]/a[r]$色的块时得到的最大的值。

但是单靠这个是不足以转移的,

另一个状态$g[l][r]$表示,不管以什么方法,删光$l~r$这段的得到的最大的值。

然后进行区间DP,显然要枚举区间,枚举断点,转移就是;

$$f[l][r][k]=max(f[l][r][k],f[l][r][k-1]+g[k'-1][r-1]);$$

这个转移就是把区间$l~r$中花式删掉$k'~r-1$这段,剩下的组成$k$个的最大方案。

$$g[l][r]=max(g[l][r],f[l][r][k]+k*k,g[l][k']+g[k'+1][r]);$$

这个转移比较显然..

然后这样转移之后,显然$f[l][r][0]=g[l][r]$.

答案就是$f[1][N][0]/g[1][N]$,时间复杂度是$<=O(N^4)$的,而且在正常的数据下表现非常优秀。

Code

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
inline int read()
{
    int x=0,f=1; char ch=getchar();
    while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
    while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}
int T,N,a[210],f[210][210][210],g[210][210],t,cnt[210][210];
int main()
{
    T=read();
    while (T--)
        {
            N=read();
            for (int i=1; i<=N; i++) a[i]=read();
            memset(cnt,0,sizeof(cnt));
            for (int i=1; i<=N; i++)
                for (int j=i+1; j<=N; j++)
                    if (a[i]==a[j])
                        for (int k=i; k<=j; k++)
                            if (a[k]==a[i]) cnt[i][j]++;
            memset(f,128,sizeof(f)); memset(g,0,sizeof(g));
            for (int i=1; i<=N; i++) g[i][i]=f[i][i][0]=1,f[i][i][1]=0; 
            for (int len=1; len<=N; len++)
                for (int l=1; l+len-1<=N; l++)
                    {
                        int r=l+len-1;
                        for (int i=1; i<=cnt[l][r]; i++)
                            {
                                for (int j=l; j<r; j++)
                                    f[l][r][i]=max(f[l][r][i],f[l][j][i-1]+g[j+1][r-1]);
                                g[l][r]=max(g[l][r],f[l][r][i]+i*i);
                            }
                        for (int i=l; i<r; i++) g[l][r]=max(g[l][r],g[l][i]+g[i+1][r]);
                        f[l][r][0]=g[l][r];
                    }
            printf("Case %d: %d\n",++t,f[1][N][0]);
        }
    return 0;
}

 

转载于:https://www.cnblogs.com/DaD3zZ-Beyonder/p/6005384.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值