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
2
4
1 2 1 2
7
1 2 1 1 3 2 1
Sample Output
Case 1: 3
Case 2: 4
题意:参加n个聚会,给出在n个聚会上要穿的衣服,数字相同代表是相同的衣服,可以穿很多件衣服,等到需要这件衣服的时候就脱下来,但是该衣服一旦被脱下来,这件衣服就脏了。求最少需要穿几件衣服。
题解
:dp[l][r]代表在[l,r]这个区间里最少需要穿的衣服。先把dp[i][i]初始化为1,意为最差的情况,就是每一个聚会都需要穿一件衣服。即:
dp[l][r]=dp[l][r-1]+1;这是初始化。
需要找到一个k,当a[k]==a[r]时,说明需要把k到r区间的衣服全部脱掉。
动态转移方程:
dp[l][r]=min(dp[l][r],dp[l][k]+dp[k+1][r-1]);
AC代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
typedef long long ll;
int a[110];
int dp[110][110];
int main()
{
int t;
int cas=1;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int i,j,k;
for(i=1; i<=n; i++)
scanf("%d",&a[i]);
for(i=1; i<=n; i++)
dp[i][i]=1;//初始化,最差的情况
int len;
int l,r;
//区间长度从2开始
for(len=2; len<=n; len++)
{
for(i=1; i+len-1<=n; i++)
{
l=i,r=i+len-1;
dp[l][r]=dp[l][r-1]+1;
for(k=l; k<r; k++)
{
if(a[k]==a[r])
dp[l][r]=min(dp[l][r],dp[l][k]+dp[k+1][r-1]);
}
}
}
printf("Case %d: %d\n",cas++,dp[1][n]);
}
return 0;
}