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.从大到小记忆化搜索,然后再回溯回去。
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
#include
#include
#include
#include
#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;
}
我们还可以用记忆化搜索还做这个题
#include
#include
#include
#include
#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
*/