[bzoj1032][JSOI2007]祖码Zuma【区间dp】
【题目链接】
https://www.lydsy.com/JudgeOnline/problem.php?id=1032
【题解】
由于标算有误在此不写题解。
/* --------------
user Vanisher
problem bzoj-1032
----------------*/
# include <bits/stdc++.h>
# define ll long long
# define inf 0x3f3f3f3f
# define N 510
using namespace std;
int read(){
int tmp=0, fh=1; char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') fh=-1; ch=getchar();}
while (ch>='0'&&ch<='9'){tmp=tmp*10+ch-'0'; ch=getchar();}
return tmp*fh;
}
int n,h[N],r[N],f[N][N],l[N],c[N],cnt,las;
int main(){
n=read();
for (int i=1; i<=n; i++)
h[i]=read();
las=-1;
for (int i=1; i<=n; i++){
if (h[i]!=las){
l[++cnt]=i;
r[cnt-1]=i-1;
c[cnt]=las=h[i];
}
}
r[cnt]=n;
memset(f,inf,sizeof(f));
for (int i=1; i<=cnt; i++)
if (l[i]==r[i])
f[i][i]=2;
else f[i][i]=1;
for (int t=1; t<cnt; t++)
for (int i=1; i<=cnt-t; i++){
int j=i+t;
for (int k=i; k<j; k++)
f[i][j]=min(f[i][j],f[i][k]+f[k+1][j]);
if (c[i]==c[j]){
if (l[i]==r[i]&&l[j]==r[j])
f[i][j]=min(f[i][j],f[i+1][j-1]+1);
else f[i][j]=min(f[i][j],f[i+1][j-1]);
}
}
printf("%d\n",f[1][cnt]);
return 0;
}