# BZOJ 1032 祖玛

## Sample Input

9
1 1 2 2 3 3 2 1 1

1

## Source

 1 #include<iostream>
2 #include<algorithm>
3 #include<cstdio>
4 #include<cstring>
5 #include<cstdlib>
6 using namespace std;
7
8 #define maxn (510)
9 int cnt,n,tot,seq[maxn],f[maxn][maxn];
10 pair <int,int> com[maxn];
11
12 int main()
13 {
14     freopen("1032.in","r",stdin);
15     freopen("1032.out","w",stdout);
16     scanf("%d",&n);
17     for (int i = 1;i <= n;++i) scanf("%d",seq+i);
18     int last = seq[1],num = 0;
19     for (int i = 1;i <= n;++i)
20     {
21         if (seq[i] != last)
22         {
23             com[++tot] = make_pair(num,last);
24             num = 1; last = seq[i];
25         }
26         else ++num;
27     }
28     com[++tot] = make_pair(num,last);
29     memset(f,0x7,sizeof(f));
30     for (int i = 1;i <= tot;++i)
31     {
32         if (com[i].first > 1) f[i][i] = 1;
33         else f[i][i] = 2;
34     }
35     for (int k = 2;k <= tot;++k)
36         for (int i = 1;i + k - 1 <= tot;++i)
37         {
38             if (com[i].second == com[i+k-1].second)
39                 f[i][i+k-1] = f[i+1][i+k-2]+(com[i].first+com[i+k-1].first <= 2?1:0);
40             for (int j = i;j < i+k-1;++j)
41                 f[i][i+k-1] = min(f[i][i+k-1],f[i][j]+f[j+1][i+k-1]);
42         }
43     printf("%d",f[1][tot]);
44     fclose(stdin); fclose(stdout);
45     return 0;
46 }
View Code

posted @ 2015-02-03 20:02  lmxyy  阅读(628)  评论(0编辑  收藏  举报