# bzoj1032 [JSOI2007]祖码Zuma

## 1032: [JSOI2007]祖码Zuma

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 672  Solved: 335
[Submit][Status][Discuss]

## Sample Input

9
1 1 2 2 3 3 2 1 1

1

## Source

Dp[i][j] = min(Dp[i][k]+Dp[k+1][j]);

1 2 1 3 1

 1 #include <cstdio>
2 #include <cstring>
3 #include <cstdlib>
4 #include <cmath>
5 #include <deque>
6 #include <vector>
7 #include <queue>
8 #include <iostream>
9 #include <algorithm>
10 #include <map>
11 #include <set>
12 #include <ctime>
13 using namespace std;
14 typedef long long LL;
15 typedef double DB;
16 #define For(i, s, t) for(int i = (s); i <= (t); i++)
17 #define Ford(i, s, t) for(int i = (s); i >= (t); i--)
18 #define Rep(i, t) for(int i = (0); i < (t); i++)
19 #define Repn(i, t) for(int i = ((t)-1); i >= (0); i--)
20 #define rep(i, x, t) for(int i = (x); i < (t); i++)
21 #define MIT (2147483647)
22 #define INF (1000000001)
23 #define MLL (1000000000000000001LL)
24 #define sz(x) ((int) (x).size())
25 #define clr(x, y) memset(x, y, sizeof(x))
26 #define puf push_front
27 #define pub push_back
28 #define pof pop_front
29 #define pob pop_back
30 #define ft first
31 #define sd second
32 #define mk make_pair
33 inline void SetIO(string Name) {
34     string Input = Name+".in",
35     Output = Name+".out";
36     freopen(Input.c_str(), "r", stdin),
37     freopen(Output.c_str(), "w", stdout);
38 }
39
40 const int N = 510;
41 int m, Arr[N], n, Color[N], Num[N];
42 int Dp[N][N];
43 bool Visit[N][N];
44
45 inline void Input() {
46     scanf("%d", &m);
47     For(i, 1, m) scanf("%d", Arr+i);
48 }
49
50 inline int Search(int L, int R) {
51     if(Visit[L][R]) return Dp[L][R];
52     Visit[L][R] = 1;
53     if(L == R) return Dp[L][R] = max(3-Num[L], 1);
54     if(L > R) return Dp[L][R] = INF;
55
56     int Ret = INF, tmp;
57     For(i, L, R-1) {
58         tmp = Search(L, i)+Search(i+1, R);
59         Ret = min(Ret, tmp);
60     }
61     if(Color[L] == Color[R]) {
62         tmp = max(3-Num[L]-Num[R], 0)+Search(L+1, R-1);
63         Ret = min(Ret, tmp);
64     }
65     return Dp[L][R] = Ret;
66 }
67
68 inline void Solve() {
69     Arr[0] = -1;
70     For(i, 1, m) {
71         if(Arr[i] != Arr[i-1]) Color[++n] = Arr[i];
72         Num[n]++;
73     }
74
75     int Ans = Search(1, n);
76     printf("%d\n", Ans);
77 }
78
79 int main() {
80     #ifndef ONLINE_JUDGE
81     SetIO("1032");
82     #endif
83     Input();
84     Solve();
85     return 0;
86 }
View Code

posted @ 2015-09-14 19:24  yanzx6  阅读(175)  评论(0编辑  收藏  举报