poj 1390 Blocks (经典区间dp 方块消除)


Blocks

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 4250   Accepted: 1704

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]

题意:

一排有颜色的方块,每次可以消除相邻的颜色相同的块,得分为方块个数的平方,消除后剩下的方块会合并,问怎样消除方块使得总得分最大。

思路:

黑书原题(p123),合并初始相邻相同的块,得到颜色数组c和对应的长度len,dp[i][j][k]表示i~j区间,与后面k个相同颜色块一起消除得分的最大值(当然k个块的颜色必须与j相同),考虑len[j]和k这一段怎么消除,有两种可能:

1.单独消除,dp[i][j][k]=dp[i][j-1][0]+(len[j]+k)^2;

2.和前面的一起消除,假设前面的一起消除的块最后一块为p,那么dp[i][j][k]=dp[i][p][k+len[j]]+dp[p+1][j-1][0]。

可以根据p和j的颜色相同以及k的范围来优化一下。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define maxn 205
#define MAXN 200005
#define INF 0x3f3f3f3f
#define mod 1000000007
#define eps 1e-6
const double pi=acos(-1.0);
typedef long long ll;
using namespace std;

int n,m,ans,tot;
int a[maxn],c[maxn],len[maxn],pos[maxn],last[maxn];
int dp[205][205][205],num[maxn][maxn];

void solve()
{
    int i,j,k,p;
    memset(pos,0,sizeof(pos));
    for(i=1;i<=tot;i++)
    {
        last[i]=pos[c[i]];
        pos[c[i]]=i;
    }
    memset(num,0,sizeof(num));
    for(i=tot;i>=1;i--)
    {
        for(j=1;j<=n;j++)
        {
            if(j==c[i]) num[j][i]=num[j][i+1]+len[i];
            else num[j][i]=num[j][i+1];
        }
    }
    memset(dp,0,sizeof(dp));
    for(int l=1;l<=tot;l++)
    {
        for(i=1;i<=tot;i++)
        {
            j=i+l-1;
            if(j>tot) break ;
            for(k=0;k<=num[c[j]][j+1];k++)
            {
               dp[i][j][k]=dp[i][j-1][0]+(len[j]+k)*(len[j]+k);
               for(p=last[j];p>=i;p=last[p])
               {
                   dp[i][j][k]=max(dp[i][j][k],dp[i][p][len[j]+k]+dp[p+1][j-1][0]);
               }
            }
        }
    }
    ans=dp[1][tot][0];
}
int main()
{
    int i,j,test,ca=0;
    scanf("%d",&test);
    while(test--)
    {
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        tot=0;
        memset(len,0,sizeof(len));
        for(i=1;i<=n;)
        {
            tot++;
            c[tot]=a[i];
            while(i<=n&&a[i]==c[tot]) i++,len[tot]++;
        }
        solve();
        printf("Case %d: %d\n",++ca,ans);
    }
    return 0;
}
时间: 2025-01-16 08:38:14

poj 1390 Blocks (经典区间dp 方块消除)的相关文章

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

uva1626 poj 1141 Brackets Sequence 区间dp 打印路径

// poj 1141 Brackets Sequence // 也是在紫书上看的一题,uva就是多了一个t组数据. // 经典区间dp // dp(i,j)表示区间[i,j]内所需要增加的括号数目 // 则分为两种情况 // 一种是s[i]和s[j]是匹配的则 // dp[i][j] = min(dp[i][j],dp[i+1][j-1]) // 另外一种情况是不匹配 // dp[i][j] = min(dp[i][j],dp[i][k]+dp[k+1][j]){i<k<j}; // 但是无

Blocks题解(区间dp)

Blocks题解 区间dp 阅读体验...https://zybuluo.com/Junlier/note/1289712 很好的一道区间dp的题目(别问我怎么想到的) dp状态 其实这个题最难的地方是这道题目的状态怎么设 首先既然是区间dp,那肯定最先想到的状态是 $dp[i][j]$表示消掉区间$[i,j]$上所有的块的最大分数 突然发现这个状态会受区间外和$i$或$j$颜色相同的块的影响 并且转移也并不好转移=_= 所以我们考虑换一种状态... 既然说会受到外面的块的影响?那考虑一种方法来

POJ 1141 Brackets Sequence (区间dp 记录路径)

题目大意: 给出一种不合法的括号序列,要求构造出一种合法的序列,使得填充的括号最少. 思路分析: 如果只要求输出最少的匹配括号的数量,那么就是简单的区间dp dp[i][j]表示 i - j 之间已经合法了最少添加的括号数. 转移 就是 dp[i] [j] = min  (dp[i+1][j]+1 , dp[ i+ 1] [ k -1 ] + dp[k+1] [j] (i k 位置的括号匹配)) 其次我们要记录路径,你发现  如果 dp [i] [j] 是由 dp [i+1] [j] 转移过来的

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 3280 Cheapest Palindrome ( 区间DP &amp;&amp; 经典模型 )

题意 : 给出一个由 n 中字母组成的长度为 m 的串,给出 n 种字母添加和删除花费的代价,求让给出的串变成回文串的代价. 分析 :  原始模型 ==> 题意和本题差不多,有添和删但是并无代价之分,要求最少操作几次才能是其变成回文串 ① 其实细想之后就会发现如果没有代价之分的话删除和增添其实是一样的,那么除了原串原本拥有的最长回文串不用进行考虑处理,其他都需要进行删除或者增添来使得原串变成回文串,所以只要对原串 S 和其反向串 S' 找出两者的最长公共子串长度 L 再用总长减去 L 即可 ②

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先和前面的一样的颜色

【Uva10559】Blocks(区间DP)

Description 题意:有一排数量为N的方块,每次可以把连续的相同颜色的区间消除,得到分数为区间长度的平方,然后左右两边连在一起,问最大分数为多少. \(1\leq N\leq200\) Solution 区间DP,对于一个连续的同色区间,可以直接消掉,或者从左边或者右边搞到和它同色的区间和在一起再一起消掉. 读入序列时预处理一下,将各个连续同色区间处理为一个点,记录它的颜色和长度,便于处理 然后就是区间DP啦,虽然要表示左边和右边,但是左边状态也可以表示为左边序列的右边,就只要开3维就行