CSU-ACM2017暑假训练9-区间DP C

来源:互联网 发布:汉奇中走丝编程视频 编辑:程序博客网 时间:2024/05/19 14:53

C - Halloween Costumes

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

241 2 1 271 2 1 1 3 2 1

Sample Output

Case 1: 3Case 2: 4

主角尬噗可以在参加完第一场聚会后,通过穿上一另件衣服,参加另一聚会,再脱下衣服的方法参加与第一场聚会dress code相同的第三场聚会,从而重复利用了第一件衣服。据此,我们可以考虑他在第 i 场聚会时要不要穿上新装。
先假定他需要穿新装,那么第 i 场时用到的衣服总量就等于前 i1 场的衣服总量加一,这个数值记为 q 。然后检查前 i1 场中有没有与第 i 场dress code相同的场次 k,若有的话,第 i 场则不需新装,在第 k 场和第 i 场之间的衣服不论需要多少件,都可以套在第 i1 场时尬噗所穿的衣服之上。于是我们将前 k 场需要的衣服总数加上第 k+1 到第 i1 场所需要的衣服总数加起来,与 q 比较大小,取较小者。以此类推。
状态转移方程:

dp[s][e]=min{dp[s][e],  dp[s][k]+dp[k+1][e1]}

#include <iostream>#include <cstdio>#include <algorithm>#include <vector>#include <cstring>#include <queue>using namespace std;int sample[104], dp[104][104];int main(){#ifdef TESTfreopen("test.txt", "r", stdin);#endif // TEST   int T, n, Cas = 1;   cin >> T;   while(T--){       memset(dp, 0, sizeof(dp));       cin >> n;       for(int i = 0; i < n; i++){           cin >> sample[i];           dp[i][i] = 1;       }       for(int len = 1; len < n; len++){           for(int s = 0, e = len; e < n; s++, e++){               dp[s][e] = dp[s][e-1]+1;               for(int k = s; k < e; k++){                   if(sample[e] == sample[k])                       dp[s][e] = min(dp[s][e], dp[s][k]+dp[k+1][e-1]);               }           }       }       printf("Case %d: %d\n", Cas++, dp[0][n-1]);   }   return 0;}
原创粉丝点击