【BZOJ 1032】 [JSOI2007]祖码Zuma

【题目链接】:http://www.lydsy.com/JudgeOnline/problem.php?id=1032

【题意】

【题解】

/*
    设f[i][j]表示从第i个珠子开始的j个珠子被消除;
    做的时候把相同颜色的且相邻的柱子合在一起;
    需要弹射几个珠子;
    f[i][1]=a[i].num==1?2:1;//如果这个合并后的珠子的个数为1则需要再加两个否则都是加1个就好;
    然后就是枚举长度,枚举起点.枚举分割点.然后处理一下可以连续消除的情况就好;
    但是程序不完善不能通过一些数据;
    但BZOJ上的数据很弱吧(数据答案都错了)。都能过呢。
*/


【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)

typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 510;
const int INF = 0x3f3f3f3f;

int n, j, f[N][N];

struct abc
{
    int cor, num;
}a[N];

void in()
{
    rei(n);
    rep1(i, 1, n)
    {
        int t;
        rei(t);
        if (i == 1 || t != a[j].cor)
        {
            a[++j].cor = t;
            a[j].num = 1;
        }
        else
            a[j].num++;
    }
}

void do_dp()
{
    memset(f, INF, sizeof f);
    n = j;
//  printf("%d
", n);
    rep1(i, 1, n)
        f[i][1] = (a[i].num == 1)? 2 : 1;
    rep1(l,2,n)
        rep1(i, 1, n)
        {
            if (i + l-1 > n)
                break;
            if (l-2>0 && a[i].cor == a[i + l - 1].cor)
                f[i][l] = f[i + 1][l - 2] + (a[i].num + a[i + l - 1].num == 2 ? 1 : 0);
            rep1(k, 1, l-1)
                f[i][l] = min(f[i][l],f[i][k] + f[i + k][l - k]);
        }
}

void o()
{
    printf("%d
", f[1][n]);
}

int main()
{
    //freopen("F:\rush.txt", "r", stdin);
    in();
    do_dp();
    o();
    //printf("
%.2lf sec 
", (double)clock() / CLOCKS_PER_SEC);
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626537.html