Description

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 (≤ ), denoting the number of test cases.

Each case starts with a line containing an integer N( ≤ N ≤ ) denoting the number of parties. Next line contains N integers, where the ith integer ci( ≤ ci ≤ ) denotes the costume he will be wearing in party i. He will attend party  first, then party , and so on.

Output

For each case, print the case number and the minimum number of required costumes.

Sample Input


Sample Output

Case : 

Case : 

题意:有N个宴会,对于每一个宴会,女猪脚都要穿一种礼服,礼服可以套着穿,但是脱了的不能再用,参加宴会必须按顺序来,从第一个到第N个,问参加这些宴会最少需要几件礼服

        比如说第一个样例:第一天穿衣服“1”,然后不脱,在第二天穿上第二件衣服“2”,第三天脱掉衣服“2”,第四天穿上新的衣服“2”,总共要3件衣服。

思路:区间dp,从后往前推

        首先每增加一天要增加一件衣服,dp[i][j]=dp[i+1][j]+1.

        然后k在i+1~j的区间,若a[i]==a[k]说明衣服可以不用脱下来,可以穿着在k天的时候再穿,所以只需要选一件即可。所有转移方程为:dp[i][j]=min(dp[i][j],dp[i+1][k-1]+dp[k][j])

        再看边界问题:有两种初始化的方法:

        1、for(int i=1;i<=n;i++){

               dp[i][i]=1;
           }即每一天需要一件衣服

        2、区间的DP值 可以设为 i,j的区间长度即可,

       for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
dp[i][j] = j - i + 1;

AC代码:

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 106
#define inf 1e12
int n;
int a[N],dp[N][N];
int main()
{
int t;
int ac=;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
} for(int i=;i<=n;i++){
dp[i][i]=;
}
for(int len=;len<n;len++){
for(int i=;i+len<=n;i++){
int j=i+len;
dp[i][j]=dp[i+][j]+;
for(int k=i+;k<=j;k++){
if(a[i]==a[k]){
dp[i][j]=min(dp[i][j],dp[i+][k-]+dp[k][j]);
}
}
}
}
printf("Case %d: ",++ac);
printf("%d\n",dp[][n]);
}
return ;
}

LightOJ - 1422 Halloween Costumes (区间dp)的更多相关文章

  1. LightOJ - 1422 Halloween Costumes —— 区间DP

    题目链接:https://vjudge.net/problem/LightOJ-1422 1422 - Halloween Costumes    PDF (English) Statistics F ...

  2. LightOJ 1422 Halloween Costumes 区间dp

    题意:给你n天需要穿的衣服的样式,每次可以套着穿衣服,脱掉的衣服就不能再穿了,问至少要带多少条衣服才能参加所有宴会 思路:dp[i][j]代表i-j天最少要带的衣服 从后向前dp 区间从大到小 更新d ...

  3. light oj 1422 Halloween Costumes (区间dp)

    题目链接:http://vjudge.net/contest/141291#problem/D 题意:有n个地方,每个地方要穿一种衣服,衣服可以嵌套穿,一旦脱下的衣服不能再穿,除非穿同样的一件新的,问 ...

  4. Light OJ 1422 - Halloween Costumes(区间DP 最少穿几件)

    http://www.cnblogs.com/kuangbin/archive/2013/04/29/3051392.html http://www.cnblogs.com/ziyi--caolu/a ...

  5. LightOj 1422 Halloween Costumes(区间DP)

    B - Halloween Costumes Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit ...

  6. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

  7. LightOJ 1422 Halloween Costumes 【 区间dp 】

    区间dp的第一题----- 看题解看了好多~~终于看懂了---55555 dp[i][j] 表示第i天到第j天至少需要多少件衣服 那么第i件衣服只被第i天占用的话, dp[i][j] = dp[i+1 ...

  8. LightOJ 1422 Halloween Costumes (区间DP,经典)

    题意: 有个人要去参加万圣节趴,但是每到一个趴都要换上特定的服装,给定一个序列表示此人要穿的衣服编号(有先后顺序的),他可以套很多件衣服在身上,但此人不喜欢再穿那些脱下的衣服(即脱下后就必须换新的), ...

  9. 【LightOJ 1422】Halloween Costumes(区间DP)

    题 题意 告诉我们每天要穿第几号衣服,规定可以套好多衣服,所以每天可以套上一件新的该号衣服,也可以脱掉一直到该号衣服在最外面.求最少需要几件衣服. 分析 DP,dp[i][j]表示第i天到第j天不脱第 ...

随机推荐

  1. UI产品设计流程中的14个要点

    http://www.sj33.cn/digital/wyll/201404/38318.html 自从我在 Dribbble 上贴了一幅我的产品设计成果,受到了大家伙热烈的反馈,对此我深受鼓励,我决 ...

  2. 为何visua studio看不到C++项目的LOG?

    最近工程中添加了一个用C++编写的项目 它作为了我正式使用项目的引用 但是当我debug的时候 居然没有看到应该有的LOG 最后找到了解决方法,如下图所示: 右击你的正式项目,属性 改变调试器类型中的 ...

  3. linux内核交互,设备驱动控制管理接口

    1,ioctl preface--starting point ,format,mount volume,in addition to the above file system -- allows ...

  4. Xcoder 7.0 免证书真机测试

    相信大家已经看了WWDC大会上的内容了,在iOS9和Xcoder7.0以后真机测试不需要在购买付费账号了,(当然你要想上传appstore还是需要付费账号的). 今天我带大家来看下免证书的真机测试如何 ...

  5. hdu 2059 龟兔赛跑(dp)

    龟兔赛跑 Problem Description 据说在很久很久以前,可怜的兔子经历了人生中最大的打击——赛跑输给乌龟后,心中郁闷,发誓要报仇雪恨,于是躲进了杭州下沙某农业园卧薪尝胆潜心修炼,终于练成 ...

  6. NSDictionary所有API的学习。

    <欢迎大家增加iOS开发学习交流群:QQ529560119> @property (readonly)NSUInteger count; //1.利用指定的key寻找相应的value - ...

  7. spark安装mysql与hive

    第一眼spark安装文件夹lib\spark-assembly-1.0.0-hadoop2.2.0.jar\org\apache\spark\sql下有没有hive文件夹,假设没有的话先下载支持hiv ...

  8. RadioButtonList选择事件onclick

    <asp:RadioButtonList ID="rbtnFInvoiceType" runat="server" onclick="check ...

  9. 牛掰的图片等比缩放js代码

    function resizeImg(img,oAW,oAH){ var oimgW = img.width, oimgH = img.height, oimg = img, oY = (oimgH/ ...

  10. C# 获取类似java gettime() 的时间格式

    今天做了一个面向Java的接口,需要做到时间的统一,C#提供了System.DateTime.UtcNow 但是需要自己做下处理,记录一下自己的方法, 留着以后查阅方便. /// <summar ...