# Bzoj1032 [JSOI2007]祖码Zuma

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 1003  Solved: 512

## Sample Input

9
1 1 2 2 3 3 2 1 1

1

## Source

 1 #include<iostream>
2 #include<algorithm>
3 #include<cstring>
4 #include<cstdio>
5 #include<cmath>
6 using namespace std;
7 const int mxn=605;
9     int x=0,f=1;char ch=getchar();
10     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
11     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
12     return x*f;
13 }
14 int n;
15 int a[mxn],c[mxn],cnt=0;
16 int f[mxn][mxn];
17 void solve(){
18     int i,j,k;
19     memset(f,0x3f,sizeof f);
20     for(int i=1;i<=n;i++)
21         f[i][i]=(c[i]>=2)?1:(3-c[i]);
22     for(int len=2;len<=n;len++){
23         for(int i=1;i<=n;i++){
24             j=i+len-1;if(j>n)break;
25             for(int k=i;k<j;k++)
26                 f[i][j]=min(f[i][j],f[i][k]+f[k+1][j]);
27             if(a[i]==a[j]){
28                 f[i][j]=min(f[i][j],f[i+1][j-1]+(c[i]+c[j]<3));
29             }
30         }
31     }
32     printf("%d\n",f[1][n]);
33     return;
34 }
35 int main(){
36     int i,j,x;
38     for(i=1;i<=n;i++){
40         if(x==a[cnt])c[cnt]++;
41         else{
42             a[++cnt]=x;
43             c[cnt]=1;
44         }
45     }
46     n=cnt;
47     solve();
48     return 0;
49 }

posted @ 2017-07-07 21:25  SilverNebula  阅读(362)  评论(0编辑  收藏  举报