UVA 10559 Blocks(记忆化dp)

这题在poj上做过,之前的博客已经有详细解释了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define ss(x) scanf("%d",&x)
const int maxn=200+10;
struct blocks{
    int amount,color;
};
int n;
int a[maxn];
vector<blocks> v;
int f[maxn][maxn][maxn];
int dp(int l,int r,int ex_len)
{
    if(f[l][r][ex_len]>=0) return f[l][r][ex_len];
    int ans=(v[r].amount+ex_len)*(v[r].amount+ex_len);
    if(l==r) return ans;
    ans+=dp(l,r-1,0);
    rep(i,l,r-1){
        if(v[i].color!=v[r].color) continue;
        ans=max(ans,dp(l,i,v[r].amount+ex_len)+dp(i+1,r-1,0));
    }
    return f[l][r][ex_len]=ans;
}
int main()
{
    int kase=0;
    int t;
    ss(t);
    while(t--)
    {
        v.clear();
        ss(n);
        rep(i,1,n) ss(a[i]);
        rep(i,1,n) {if(a[i]!=a[i-1]) {v.push_back((blocks){1,a[i]});}else v[v.size()-1].amount++;}
        memset(f,-1,sizeof(f));
        printf("Case %d: %d\n",++kase,dp(0,(int)v.size()-1,0));
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值