light oj 1422 区间dp

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 #include <math.h>
 5 #include <iostream>
 6 #include <algorithm>
 7 #include <climits>
 8 #include <queue>
 9 #define ll long long
10 
11 using namespace std;
12 
13 int color[105],n;
14 int dp[105][105];
15 
16 int dfs(int l,int r)
17 {
18     if(r < l) return 0;
19     if(dp[l][r] != -1)
20         return dp[l][r];
21     if(l == r)
22         return dp[l][r] = 1;
23     dp[l][r] = r-l+1;
24 
25     dp[l][r] = min(dfs(l+1,r)+1,dp[l][r]); //新穿一件
26 
27     for(int i = l+1; i <= r; i++)
28     {
29         if(color[l] == color[i])
30             dp[l][r] = min(dp[l][r],dfs(l+1,i-1)+dfs(i,r)); //l == i 不用新穿
31     }
32     return dp[l][r];
33 }
34 
35 void solve()
36 {
37     scanf("%d",&n);
38     memset(dp,-1,sizeof(dp));
39     for(int i = 1; i <= n; i++)
40         scanf("%d",&color[i]);
41     int ans = dfs(1,n);
42 
43     printf("%d\n",ans);
44 }
45 
46 int  main(void)
47 {
48 
49     int t,cnt = 0;
50     scanf("%d",&t);
51     while(t--)
52     {
53         printf("Case %d: ",++cnt);
54         solve();
55     }
56     return 0;
57 }

 

posted on 2016-08-13 17:05  鱼泪儿  阅读(167)  评论(0编辑  收藏  举报

导航