【POJ-1390】Blocks 区间DP

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

Liu [email protected]

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;
}
时间: 2024-10-13 23:28:17

【POJ-1390】Blocks 区间DP的相关文章

poj 1390 Blocks 区间DP

Description: 给你一堆方块,颜色相同可以消去,每次消去的价值为消去方块的个数的平方.单个方块可以消去.问你最后收获的最大价值为多少 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 using namespace std; 5 const int N = 220; 6 int dp[N][N][N], g[N][N], col[N]; 7 //dp[i][j][k]表示i-j这一段消得

POJ 1390 Blocks(DP + 思维)题解

题意:有一排颜色的球,每次选择一个球消去,那么这个球所在的同颜色的整段都消去(和消消乐同理),若消去k个,那么得分k*k,问你消完所有球最大得分 思路:显然这里我们直接用二位数组设区间DP行不通,我们不能表示出“合并”这种情况.我们先把所有小块整理成连续的大块. 我们用click(l,r,len)表示消去l到r的所有大块和r后len块和r颜色一样的小块的最大得分.那么这样我们可以知道,click(l,r,len)只有两种情况: 1.r直接和后面len全都消去 2.r带着len先和前面的一样的颜色

[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 belo

POJ 1390 Blocks(记忆化搜索+动态规划)

POJ 1390 Blocks 砌块 时限:5000 MS   内存限制:65536K 提交材料共计: 6204   接受: 2563 描述 你们中的一些人可能玩过一个叫做"积木"的游戏.一行有n个块,每个盒子都有一个颜色.这是一个例子:金,银,铜,金.相应的图片如下: 图1如果一些相邻的盒子都是相同的颜色,并且它左边的盒子(如果它存在)和它的右边的盒子(如果它存在)都是其他颜色的,我们称它为"盒子段".有四个盒子段.那就是:金,银,铜,金.片段中分别有1,4,3,

POJ 1179 Polygon 区间DP

链接:http://poj.org/problem?id=1179 题意:给出一个多边形,多边形的每个顶点是一个数字,每条边是一个运算符号"+"或者"x".要求的过程如下,手下移除一条边,即这条边不做运算.之后每次移除一条边,将其两边的数字进行对应边的运算,用得到的数字来替代原来的两个点.要求所有边都移除以后得到的最大的答案. 思路:典型的区间DP,在过程中每次操作的处理方式为dp_max[i][j]=dp[i][k]*dp[k+1][j],dp_max[i][j]

POJ 2955 Brackets (区间DP)

题意:给定一个序列,问你最多有多少个合法的括号. 析:区间DP,dp[i][j] 表示在 第 i 到 第 j 区间内最多有多少个合法的括号. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <ios

POJ 1160 (区间DP+四边形优化)

这个转移方程不好想,尤其是一段值的解是中间,不明觉厉.dp[i][j] 用i个邮局,覆盖前j个村庄的最小值. 还有就是区间dp的平行四边形优化,这个题的转移方程并不是"区间DP",所以枚举状态要逆着(很花时间),且用一个邮局覆盖都是从0断开了相当于没有断开. 类比于石子归并,矩阵链乘等标准区间DP,其所需状态之前就已经获得,不用倒推 #include <cstdio> #include <cstring> #include <iostream> us

POJ 2955-Brackets(区间DP)

Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3340   Accepted: 1716 Description We give the following inductive definition of a "regular brackets" sequence: the empty sequence is a regular brackets sequence, if s is a reg

POJ 1159 Palindrome(区间DP/最长公共子序列+滚动数组)

Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 56150   Accepted: 19398 Description A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a