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.
InputInput 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.
OutputFor each case, print the case number and the minimum number of required costumes.
Sample Input2
4
1 2 1 2
7
1 2 1 1 3 2 1
Sample OutputCase 1: 3
Case 2: 4
感觉看这个题之前先看这个也行:here
题意:
现在那个人要去参加一个聚会,然后总共有n天,每天所要求穿的服饰的序号分别为c[i]。
这个人可以一次性穿上1件衣服,或者一次性脱下任意多件衣服。当然也可以在衣服外面套衣服。
并且如果这件衣服已经被脱下的话,那么它下次不能再次被穿上,如果我们还需要这件衣服的话,那么我们就只能重新再去买另外一件了。
最后问你,如果要参加完所有的派对,那么他所需要的最少的衣服数量是多少。
比如说第一个样例:
4
1 2 1 2
我们只需要先穿上1,然后再穿上2,再脱掉1,然后此时没有2的衣服了,所以我们还需要一件2的衣服,所以最后总共是需要3件衣服。思路:
这题主要的难点是在于判断什么时候可以利用还存在的衣服。
定义dp[i][j]为i~j区间内所需要的最少衣服的数量。
以上参考网址:here/*
* Light OJ 1422 - Halloween Costumes
* http://lightoj.com/volume_showproblem.php?problem=1422
* 区间DP的思想。
* 比如要求解i到j,dp[i][j].
* 就是考虑i的衣服,一种是i的衣服只有在i使用,那么就是dp[i+1][j]+1件
* 然后再i+1~j中枚举k,如果a[i]==a[k].那么dp[i][j]=min(dp[i][j],dp[i+1][k-1]+dp[k][j])
* 注意因为i的衣服是可以使用多次的,所以不需要加1,是dp[k][j]
* 思想很妙
很简单的区间DP的入门题。一开始这题想了很久就是想不出来。直到做了后面几道区间DP回过来终于想明白了。
区间DP可以使用记忆化搜索和直接DP的方法写。
这题的状态转移方程:这个是看的第i件衣服,有点抽象 dp[i][j]=min(1+dp[i+1][j],dp[i+1][k-1]+dp[k][j]) ( a[i]==a[k] i
#include
#include
#include
using namespace std;
const int MAXN=110;
int a[MAXN];
int dp[MAXN][MAXN];
int main()
{
int T;
int n;
scanf("%d",&T);
int iCase=0;
while(T--)
{
iCase++;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
dp[i][i]=1;
for(int i=n-1;i>=1;i--)///注意DP的顺序
for(int j=i+1;j<=n;j++)
{
dp[i][j]=dp[i+1][j]+1;///这个表示第i个的衣服在后面没有利用了
for(int k=i;k<=j;k++)
if(a[i]==a[k])///用同一件
dp[i][j]=min(dp[i][j],dp[i+1][k-1]+dp[k][j]); ///把i+1到k-1件衣服脱掉
}
printf("Case %d: %d\n",iCase,dp[1][n]);
}
return 0;
}
这个是看的第j件衣服!!!好想一些,因为看到是最后一件
/*
* Light OJ 1422 - Halloween Costumes
* http://lightoj.com/volume_showproblem.php?problem=1422
* 区间DP的思想。
* 比如要求解i到j,dp[i][j].
* 就是考虑i的衣服,一种是i的衣服只有在i使用,那么就是dp[i+1][j]+1件
* 然后再i+1~j中枚举k,如果a[i]==a[k].那么dp[i][j]=min(dp[i][j],dp[i+1][k-1]+dp[k][j])
* 注意因为i的衣服是可以使用多次的,所以不需要加1,是dp[k][j]
* 思想很妙
*/
#include
#include
#include
#include
using namespace std;
const int MAXN=110;
int a[MAXN];
int dp[MAXN][MAXN];
int main()
{
int T;
int n;
scanf("%d",&T);
int iCase=0;
while(T--)
{
iCase++;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++)
dp[i][j]=j-i+1;
for(int i=n-1;i>=1;i--)//注意DP的顺序
for(int j=i+1;j<=n;j++)
{
dp[i][j]=dp[i][j - 1]+1;//这个表示第i个的衣服在后面没有利用了
for(int k=i;k<=j - 1;k++)
if(a[j]==a[k])//用同一件
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k + 1][j - 1]);
}
printf("Case %d: %d\n",iCase,dp[1][n]);
}
return 0;
}