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 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.
For each case, print the case number and the minimum number of required costumes.
2
4
1 2 1 2
7
1 2 1 1 3 2 1
Case 1: 3
Case 2: 4
一道区间DP的题目,我还是太水了,想了好久毫无思绪,最后看了题解…题意:告诉有n场晚会中需要穿的衣服,衣服是可以套在其他衣服外面的,告诉了序列顺序之后求出最少需要穿多少次衣服。
题解:dp[i][j]表示第i个地方到第j个地方最少需要准备几件衣服,初始化dp[i][j](i <= j)为1,因为至少要1件,对于第i个地方,我们先假设其状态与后面无关即需要穿它,则dp[i][j] = dp[i+1][j] + 1,然后再从i+1向后枚举k,若c[i] == c[k],则表示之后有一个地方要穿的与第i个地方相同,则我们可以不脱它,因为第i天和第k天穿的相同,将区间分成两段,表示为dp[i][j] = dp[i+1][k - 1] + dp[k][j];
ac code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std;
#define si1(a) scanf("%d",&a)
#define si2(a,b) scanf("%d%d",&a,&b)
#define sd1(a) scanf("%lf",&a)
#define sd2(a,b) scanf("%lf%lf",&a,&b)
#define ss1(s) scanf("%s",s)
#define pi1(a) printf("%d\n",a)
#define pi2(a,b) printf("%d %d\n",a,b)
#define mset(a,b) memset(a,b,sizeof(a))
#define forb(i,a,b) for(int i=a;i<b;i++)
#define ford(i,a,b) for(int i=a;i<=b;i++)
typedef long long LL;
const int N=1100001;
const int M=6666666;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-7;
int dp[102][102];
int ans[102];
int main()
{
int T;
si1(T);
int cas=1;
while(T--)
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
si1(ans[i]);
}
mset(dp,0);
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
dp[i][j]=1;
}
for(int i=n-2;i>=0;i--)
{
for(int j=i;j<n;j++)
{
dp[i][j]=dp[i+1][j]+1;//第i天准备衣服。
for(int k=i+1;k<=j;k++)
{
if(ans[i]==ans[k])
dp[i][j]=min(dp[i][j],dp[i+1][k-1]+dp[k][j]);
}
}
}
printf("Case %d: %d\n",cas++,dp[0][n-1]);
}
return 0;
}