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的思想主要是分治。所以第一步就是划分子问题,这点尤其重要,
状态的意义也就是由此而来。
对于一个区间DP问题,我们关键要做的就是找到一个连续的,易于表示的区间。
为什么呢?因为如果找到不连续的,零散无关联的多个元素,状态的表示非常之难。
因为会占用太多空间。如果是一个连续的区间,那么就非常好表示,比如数组中连续的一段,我就可以表示为[le,ri];
实际的过程是纷繁复杂的,动态规划就是要通过合理地设计顺序,合理地表示状态,来达到简化计算量的目的。
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<fstream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<cctype>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI (4.0*atan(1.0))
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n) for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n) for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n) for(int i=(n) ;i>=0 ;i--)
#define lson ind<<1,le,mid
#define rson ind<<1|1,mid+1,ri
#define MID int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mk make_pair
#define _f first
#define _s second
#define ysk(x) (1<<(x))
using namespace std;
typedef long long ll;
//const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
const int INF =0x3f3f3f3f;
const int maxn=100+10 ;
//const int maxV=12 ;
int a[maxn];
int n;
int dp[maxn][maxn];
int DP(int le,int ri)
{
if(~dp[le][ri]) return dp[le][ri];
if(le>ri) return dp[le][ri]=0;
if(le==ri) return dp[le][ri]=1;
dp[le][ri]=DP(le,ri-1)+1;
for(int i=le;i<=ri-1;i++) if(a[i]==a[ri])
{
dp[le][ri]=min(dp[le][ri],DP(le,i)+DP(i+1,ri-1) );
}
return dp[le][ri];
}
int main()
{
int T,kase=0;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
memset(dp,-1,sizeof dp);
printf("Case %d: %d\n",++kase,DP(1,n));
}
return 0;
}