poj 1390 Blocks (动态规划)

Blocks

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 4318   Accepted: 1745

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

递归形式的动态规划:dp[st][ed][len]从st到ed完全消除,且ed右边挨着有一个len的大块颜色和ed相同.

一种消除方式是,Len块直接和ed块合并直接消除得到分数work(st,ed-1,0)+(a[ed].n+len)*(a[ed].n+len);

另一种是在st到ed之间找到一个块p和ed块颜色相同,把这3块直接合并 work(st,p,a[ed].n+len)+work(p+1,ed-1,0);

两种方式取最大的值。

当st==ed时递归结束。

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<queue>
using namespace std;
#define LL __int64
#define N 210
const int inf=0x1f1f1f1f;
struct node
{
    int c,n,p;
}a[N];
int f[N][N][N];
int work(int st,int ed,int len)
{
    if(f[st][ed][len])
        return f[st][ed][len];
    int i,ans=(a[ed].n+len)*(a[ed].n+len);
    if(st==ed)
    {
        f[st][ed][len]=ans;
        return ans;
    }
    ans+=work(st,ed-1,0);
    for(i=ed-1;i>=st;i--)
    {
        if(a[i].c!=a[ed].c)
            continue;
        int tmp=work(st,i,a[ed].n+len)+work(i+1,ed-1,0);
        if(tmp<=ans)
            continue;
        ans=tmp;
        break;
    }
    f[st][ed][len]=ans;
    return ans;
}

int main()
{
    int T,t,cnt,i,n,Cas=1;
    scanf("%d",&T);
    while(T--)
    {
        memset(a,0,sizeof(a));
        scanf("%d",&n);
        scanf("%d",&t);
        cnt=0;
        a[cnt].c=t;
        a[cnt].n=1;
        for(i=1;i<n;i++)
        {
            scanf("%d",&t);
            if(t==a[cnt].c)
            {
                a[cnt].n++;
            }
            else
            {
                cnt++;
                a[cnt].c=t;
                a[cnt].n=1;
            }
        }
        memset(f,0,sizeof(f));
        printf("Case %d: %d\n",Cas++,work(0,cnt,0));
    }
    return 0;
}
时间: 2024-08-03 08:22:19

poj 1390 Blocks (动态规划)的相关文章

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

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

[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 (记忆化搜索)

Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4318   Accepted: 1745 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, Sil

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

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 3734 Blocks

                                                         POJ 3734   Blocks Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description Panda has received an assignment of painting a line of blocks. Since Panda is such an

[POJ 3734] Blocks (矩阵快速幂、组合数学)

Blocks Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3997   Accepted: 1775 Description Panda has received an assignment of painting a line of blocks. Since Panda is such an intelligent boy, he starts to think of a math problem of paint

[ACM] POJ 2677 Tour (动态规划,双调欧几里得旅行商问题)

Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3585   Accepted: 1597 Description John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane and starts visiting beautiful places. To save money, John must