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

代码:

#include<cmath>
#include<stack>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 200 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
int num[maxn], a[maxn], p[maxn], cnt;
int dp[maxn][maxn][maxn];
//j后还有k个一样的小块
int click(int l, int r, int len){
    if(l > r) return 0;
    if(dp[l][r][len]) return dp[l][r][len];
    if(l == r) return dp[l][r][len] = (num[l] + len) * (num[l] + len);
    dp[l][r][len] = click(l, r - 1, 0) + (num[r] + len) * (num[r] + len);
    for(int i = l; i < r; i++){
        if(p[i] != p[r]) continue;
        dp[l][r][len] = max(dp[l][r][len], click(l, i, num[r] + len) + click(i + 1, r - 1, 0));
    }
    return dp[l][r][len];
}
int main(){
    int t, ca = 1;
    scanf("%d", &t);
    while(t--){
        int n;
        scanf("%d", &n);
        memset(num, 0, sizeof(num));
        memset(dp, 0, sizeof(dp));
        for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
        cnt = 0;
        a[0] = -INF;
        for(int i = 1; i <= n; i++){
            if(a[i] != a[i - 1]){
                ++cnt;
                num[cnt]++;
                p[cnt] = a[i];
            }
            else{
                num[cnt]++;
            }
        }
        printf("Case %d: %d\n", ca++, click(1, cnt, 0));
    }
    return 0;
}

原文地址:https://www.cnblogs.com/KirinSB/p/10645068.html

时间: 2024-10-16 19:59:17

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,

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

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 (动态规划)

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 1141 区间dp

题目链接:http://poj.org/problem?id=1141 题解:略 代码: #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; #define ll long long const int maxn=1e2+5; const int INF=0x3f3f3f3f; int dp[105][105]; int

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 2392 Space Elevator 背包题解

多重背包,本题不需要二分优化.相对简单点.因为重复数十分小,小于10: 而增加一个限制每种材料的高度做法,如果使用逆向填表,那么只需要从这个高度往小递归填表就可以了. 还有就是注意要排序,以限制高度为标准从小到大排序,否则答案错误的. #include <stdio.h> #include <string.h> #include <algorithm> using std::sort; const int MAX_K = 401; const int MAX_H = 4