CodeForces - 607B - Zuma(区间DP+区间dp介绍 )

本文介绍了一个CodeForces上的经典问题——Zuma游戏的最小消除次数问题,使用区间动态规划和记忆化搜索的方法进行解答。详细解释了如何通过构建dp矩阵来寻找最小的操作次数,以及如何利用记忆化搜索优化计算过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CodeForces - 607B - Zuma(区间DP+记忆化搜索)
Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color ci. The goal of the game is to destroy all the gemstones in the line as quickly as possible.

In one second, Genos is able to choose exactly one continuous substring of colored gemstones that is a palindrome and remove it from the line. After the substring is removed, the remaining gemstones shift to form a solid line again. What is the minimum number of seconds needed to destroy the entire line?

Let us remind, that the string (or substring) is called palindrome, if it reads same backwards or forward. In our case this means the color of the first gemstone is equal to the color of the last one, the color of the second gemstone is equal to the color of the next to last and so on.

Input
The first line of input contains a single integer n (1 ≤ n ≤ 500) — the number of gemstones.

The second line contains n space-separated integers, the i-th of which is ci (1 ≤ ci ≤ n) — the color of the i-th gemstone in a line.

Output
Print a single integer — the minimum number of seconds needed to destroy the entire line.

Examples
Input
3
1 2 1
Output
1
Input
3
1 2 3
Output
3
Input
7
1 4 4 2 3 2 1
Output
2
Note
In the first sample, Genos can destroy the entire line in one second.

In the second sample, Genos can only destroy one gemstone at a time, so destroying three gemstones takes three seconds.

In the third sample, to achieve the optimal time of two seconds, destroy palindrome 4 4 first and then destroy palindrome 1 2 3 2 1.

题目大意:

给你一串数,如果是回文序列,就可以一次性消掉,求最少需要消几次

解题思路:

这是个区间dp问题
所谓区间dp,就是通过小区间的最优解来解决大区间的最优解
解决区间dp有两个基本思路:
a.直接由小到大,推出递推公式
b.从大到小记忆化搜索,然后再回溯回去。

注:记忆化搜索的题目的n应该是<1e3的,如果在1e3以上的就不要考虑区间DP了

A:我们建一个二维dp[][]数组,dp[i][j]表示 从i-j 最少用的次数
然后我们就可以考虑 几个特殊情况
1.如果这个区间长度为1 的时候 这时候 肯定是1 dp[i][i]=1;
2.如果区间长度是2的话,那么如果a[i]==a[i+1],就是相邻的两个颜色相同,那这俩就可以一块消掉,这时候dp[i][i+1]=1,如果相邻的不相等,那么就得一个一个的消除了,dp[i][i+1]=2

然后就是一般情况
对于一个区间 [l,r],如果a[r]==a[l],那么消除[l+1,r-1]的时候用的时间和 [l,r]的时间可以是一样的,因为两边相等 是个回文。
然后我们再我们还可以从l-r 之间找一个k,从l消除到r 可以消除 l-k的时间 +消除k+1-r的时间。

dp[i][i+len-1]=min(dp[i][i+len-1],dp[i][k]+dp[k+1][i+len-1]) (2<=len<=n,i<=k<i+len-1)
AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#define inf 0x3f3f3f3f3f
using namespace std;
const int maxn=1e3;
int dp[maxn][maxn];//dp[i][j]表示从i到j最少需要多少秒消除
int a[maxn];///存每个点的颜色
int n;///有多少个点
int main()
{

		cin>>n;
		for(int i=1;i<=n;i++)
			cin>>a[i];
		memset(dp,inf,sizeof(dp));
		for(int len=1;len<=n;len++)
		{
			for(int l=1;l+len-1<=n;l++)
			{
				r=len+l-1;
				if(l==r)
					dp[l][r]=1;
				if(l+1==r)
				{
					if(a[l]==a[r])
						dp[l][r]=1;
					else
						dp[l][r]=2;
				}
				else 
				{
					for(int k=l+1;k<=r;k++)
						dp[l][r]=min(dp[i][j],dp[l][k]+dp[k+1][r]);
					if(a[l]==a[r])
						dp[l][r]=min(dp[l][r],dp[l+1][r-1]);
			}
		}
		cout<<dp[1][n]<<endl;
		return 0;

}

我们还可以用记忆化搜索还做这个题

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=1e3;
int dp[maxn][maxn];
int a[maxn];
int n;
void init()
{
	memset(dp,inf,sizeof(dp));
}
int dfs(int l,int r)
{
	if(l==r)
		return dp[l][r]=1;
	if(l>r)
		return 1;
	if(dp[l][r]!=inf)//之前已经更新过值了
		return dp[l][r];
	int res=inf;
	if(a[l]==a[r])
		res=min(res,dfs(l+1,r-1));
	for(int k=l;k<r;k++)
		res=min(res,dfs(l,k)+dfs(k+1,r));
	return dp[l][r]=res;
}
int main()
{
	while(cin>>n)
	{
		init();
		for(int i=1;i<=n;i++)
			cin>>a[i];
		cout<<dfs(1,n)<<endl;
	}
	return 0;
}
/*
3
1 2 1
3
1 2 3
7
1 4 4 2 3 2 1
*/
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.youkuaiyun.com/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值