LightOJ - 1422 Halloween Costumes —— 区间DP

题目链接:https://vjudge.net/problem/LightOJ-1422

 

1422 - Halloween Costumes
    PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB

Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and he is planning to attend as many parties as he can. Since it's Halloween, these parties are all costume parties, Gappu always selects his costumes in such a way that it blends with his friends, that is, when he is attending the party, arranged by his comic-book-fan friends, he will go with the costume of Superman, but when the party is arranged contest-buddies, he would go with the costume of 'Chinese Postman'.

Since he is going to attend a number of parties on the Halloween night, and wear costumes accordingly, he will be changing his costumes a number of times. So, to make things a little easier, he may put on costumes one over another (that is he may wear the uniform for the postman, over the superman costume). Before each party he can take off some of the costumes, or wear a new one. That is, if he is wearing the Postman uniform over the Superman costume, and wants to go to a party in Superman costume, he can take off the Postman uniform, or he can wear a new Superman uniform. But, keep in mind that, Gappu doesn't like to wear dresses without cleaning them first, so, after taking off the Postman uniform, he cannot use that again in the Halloween night, if he needs the Postman costume again, he will have to use a new one. He can take off any number of costumes, and if he takes off k of the costumes, that will be the last k ones (e.g. if he wears costume A before costume B, to take off A, first he has to remove B).

Given the parties and the costumes, find the minimum number of costumes Gappu will need in the Halloween night.

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case starts with a line containing an integer N (1 ≤ N ≤ 100) denoting the number of parties. Next line contains N integers, where the ith integer ci (1 ≤ ci ≤ 100) denotes the costume he will be wearing in party i. He will attend party 1 first, then party 2, and so on.

Output

For each case, print the case number and the minimum number of required costumes.

Sample Input

Output for Sample Input

2

4

1 2 1 2

7

1 2 1 1 3 2 1

Case 1: 3

Case 2: 4

 

 

 

 

题解:

给定一个区间,每次可以为一段连续的子区间刷一种颜色。问最少需要刷多少次,能得到目标的区间。经典的区间DP。

1.dp[l][r]为在区间[l, r]内最少需要刷的次数。

2.在区间[l,r]内,我们对l进行讨论:

1)如果为左端点处刷上颜色时,仅仅是刷左端点处,而不延续到后面,那么状态可以转化为:dp[l][r] = 1+dp[l+1][r];)

2)如果为左端点处刷上颜色时,还延续到后面的区间。那么就枚举颜色刷到的右端点(前提是左右端点的颜色相同)。因为右端点和左端点的颜色是在同一次刷的,那么就可以把右端点处忽略,所以就转化为:dp[l][r] = dp[l][k-1] + dp[k+1][r]。枚举k,取所有情况的最小值即可。

 

 

记忆化搜索:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <cmath>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 const int INF = 2e9;
15 const LL LNF = 9e18;
16 const int MOD = 1e9+7;
17 const int MAXN = 100+10;
18 
19 int s[MAXN];
20 int dp[MAXN][MAXN];
21 
22 int dfs(int l, int r)
23 {
24     if(l==r) return 1;
25     if(l>r) return 0;
26     if(dp[l][r]!=-1) return dp[l][r];
27 
28     dp[l][r] = 1+dfs(l+1, r);
29     for(int k = l+1; k<=r; k++)
30         if(s[l]==s[k])
31             dp[l][r] = min(dp[l][r], dfs(l, k-1)+dfs(k+1, r) );
32 
33     return dp[l][r];
34 }
35 
36 int main()
37 {
38     int T, n;
39     scanf("%d", &T);
40     for(int kase = 1; kase<=T; kase++)
41     {
42         scanf("%d", &n);
43         for(int i = 1; i<=n; i++)
44             scanf("%d", &s[i]);
45         memset(dp, -1, sizeof(dp));
46         int ans =  dfs(1, n);
47         printf("Case %d: %d\n", kase, ans);
48     }
49 }
View Code

 

递推:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <cmath>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 const int INF = 2e9;
15 const LL LNF = 9e18;
16 const int MOD = 1e9+7;
17 const int MAXN = 100+10;
18 
19 int s[MAXN];
20 int dp[MAXN][MAXN];
21 
22 int main()
23 {
24     int T, n;
25     scanf("%d", &T);
26     for(int kase = 1; kase<=T; kase++)
27     {
28         scanf("%d", &n);
29         for(int i = 1; i<=n; i++)
30             scanf("%d", &s[i]);
31 
32         memset(dp, 0, sizeof(dp));
33         for(int i = 1; i<=n; i++)
34             dp[i][i] = 1;
35         for(int len = 2; len<=n; len++)
36         {
37             for(int l = 1; l<=n-len+1; l++)
38             {
39                 int r = l+len-1;
40                 dp[l][r] = 1 + dp[l+1][r];
41                 for(int k = l+1; k<=r; k++)
42                     if(s[l]==s[k])
43                         dp[l][r] = min(dp[l][r], dp[l][k-1]+dp[k+1][r]);
44             }
45         }
46 
47         printf("Case %d: %d\n", kase, dp[1][n]);
48     }
49 }
View Code

 

 

 

写法二(虽然过了,但是想不明白为什么可行):

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <cmath>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 const int INF = 2e9;
15 const LL LNF = 9e18;
16 const int MOD = 1e9+7;
17 const int MAXN = 100+10;
18 
19 int s[MAXN];
20 int dp[MAXN][MAXN];
21 
22 int dfs(int l, int r)
23 {
24     if(l==r) return 1;
25     if(dp[l][r]!=-1) return dp[l][r];
26 
27     dp[l][r] = r-l+1;
28     if(s[l]==s[r]) dp[l][r] = dfs(l, r-1);
29     for(int k = l; k<r; k++)
30         dp[l][r] = min(dp[l][r], dfs(l, k)+dfs(k+1, r) );
31 
32     return dp[l][r];
33 }
34 
35 int main()
36 {
37     int T, n;
38     scanf("%d", &T);
39     for(int kase = 1; kase<=T; kase++)
40     {
41         scanf("%d", &n);
42         for(int i = 1; i<=n; i++)
43             scanf("%d", &s[i]);
44         memset(dp, -1, sizeof(dp));
45         int ans =  dfs(1, n);
46         printf("Case %d: %d\n", kase, ans);
47     }
48 }
View Code

 

posted on 2017-11-30 09:24  h_z_cong  阅读(419)  评论(0编辑  收藏  举报

导航