Uva 10559 & POJ 1390 Blocks 区间dp

题意:

给定n个方块,相邻同色的方块可以消除,得分为消除的个数^2

问最高得分


dp[ l ][ r ][ k ] :  表示[l,r]区间内,消掉 [l,r)区间最高分 + 消掉[r, r+k] ([r,r+k]为同色)的最高分

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<cmath>
#include<vector>
using namespace std;
#define ll int
#define N 210
ll dp[N][N][N];//dp[l][r][k] 使得区间[l,r]为空时最高分 最后一次操作为将[r,r+k]个与r同色的块消掉
ll clo[N], n;
ll dfs(ll l, ll r, ll k){
	if(l>r)return 0;
	if(dp[l][r][k])return dp[l][r][k];
	int ans = dfs(l,r-1,0) + (1+k)*(1+k);
	for(int i = r-1; i >= l; i--)
		if(clo[i] == clo[r])
		ans = max(ans, dfs(l, i, k+1)+dfs(i+1,r-1,0));
	return dp[l][r][k] = ans;
}
int main()
{
	ll i, j, k, u, v, T, Cas = 1;scanf("%d",&T);
	while(T--){
		scanf("%d",&n);
		memset(clo, -1, sizeof clo);
		for(i = 1; i <= n; i++)	scanf("%d",&clo[i]);

		memset(dp, 0, sizeof dp);
		printf("Case %d: %d\n",Cas++, dfs(1,n,0));
    }
	return 0;
}
/*
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1500

99
9
1 2 2 3 2 3 3 3 1
10
1 1 1 1 1 1 1 1 1 1 


5
1 2 2 3 4
18
1 2 2 3 2 3 2 3 2 2 2 3 3 3 2 2 2 1

30
1 2 2 3 2 3 2 3 2 2 2 3 3 3 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 

60
1 2 2 3 2 3 2 3 2 2 2 3 3 3 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 3 2 3 2 3 2 2 2 3 3 3 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 

200
2 2 2 2 2 2 2 2 2 2 1 2 2 3 2 3 2 3 2 2 2 3 3 3 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 3 2 3 2 3 2 2 2 3 3 3 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 3 2 3 2 3 2 2 2 3 3 3 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 3 2 3 2 3 2 2 2 3 3 3 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 3 2 3 2 3 2 2 2 3 3 3 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 3 2 3 2 3 2 2 2 3 3 3 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 



*/


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值