poj1390 Blocks

poj1390 题 目 传 送 门 p o j 1390


题目

Blocks B l o c k s

TimeLimit:5000MS T i m e L i m i t : 5000 M S
MemoryLimit:65536K M e m o r y L i m i t : 65536 K
TotalSubmissions:6118 T o t a l S u b m i s s i o n s : 6118
Accepted:2523 A c c e p t e d : 2523

Description D e s c r i p t i o n

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:

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:

The first one is OPTIMAL.

Find the highest score you can get, given an initial state of this game.

Input I n p u t

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 O u t p u t

For each test case, print the case number and the highest possible score.

SampleInput S a m p l e I n p u t

2
9
1 2 2 2 2 3 3 3 1
1
1

SampleOutput S a m p l e O u t p u t

Case 1: 29
Case 2: 1


题解

这道题是一道看起来很简单的难题。
由于贪心,我们可以发现:颜色一样位置相邻的块可以直接合并,预处理掉即可。
然后我们设计状态: f[i][j] f [ i ] [ j ] 表示 i i j合并后的最大分数,但是我们发现无法转移,因为存在多段颜色都一样位置不相邻的块合并的情况。
所以我们需要重新设计状态: f[i][j][k] f [ i ] [ j ] [ k ] 表示 i i j,并且在 j j 位置中加长颜色相同的k块,合并后的最大分数。
这样就容易转移了:
1. 1. 如果 j j 自己合并,那么f[i][j][k]=f[i][j1][0]+(len[j]+k)2
2. 2. 如果 j j 与别人合并,那么枚举与j一起合并的别人 p p f[i][j][k]=max(f[p+1][j1][0]+f[i][p][len[j]+k]) ip<j i ≤ p < j col[p]=col[j] c o l [ p ] = c o l [ j ]
这样就完美解决了。


标程

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 250;

int main() {
    int t; cin >> t;
    static int f[N][N][N];
    static int n, m, c[N], d[N], e[N];
    for (int tt = 1; tt <= t; tt++) {
        cin >> n; m = 0; e[0] = n;
        memset(d, 0, sizeof d);
        for (int x, i = 1; i <= n; i++) {
            cin >> x;
            if (x != c[m]) {
                c[++m] = x;
                e[m] = e[m - 1];
            }
            d[m]++; e[m]--;
        }
        for (int i = m; i; i--) {
            for (int j = i; j <= m; j++) {
                for (int k = 0; k <= e[j]; k++) {
                    f[i][j][k] = f[i][j - 1][0] + (d[j] + k) * (d[j] + k);
                    for (int l = i; l < j; l++) {
                        if (c[j] != c[l]) continue;
                        f[i][j][k] = max(f[i][j][k], f[i][l][d[j] + k] + f[l + 1][j - 1][0]);
                    }
                }
            }
        }
        cout << "Case " << tt << ": " << f[1][m][0] << endl;
    }
    return 0;
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值