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. android activity启动的时候隐藏软键盘

    1.概述 android如果界面有EditText之类的跳软键盘的控件  在跳转到该界面是默认会跳出软键盘的. 更何况有些需求要直接需要获取焦点 <requestFocus /> 如果是E ...

  2. Esper

    https://www.igvita.com/2011/05/27/streamsql-event-processing-with-esper/ http://torycatkin.iteye.com ...

  3. 浅谈38K红外发射接受编码

    之前做接触过一次红外遥控器,现在有空想用简单的话来聊一聊,下面有错误的地方欢迎改正指出:1:红外的概念不聊,那是一种物理存在.以下聊38K红外发射接收,主要讲可编程的红外编码.2:红外遥控 红外遥控首 ...

  4. Chapter 5. Label and Entry Widgets 标签和输入部件

    Chapter 5. Label and Entry Widgets  标签和输入部件 有时候,你需要用户输入特定的信息,比如他们的名字,地址或者 甚至序号. 简单的方式来实现这个是使用Enry 部件 ...

  5. C语言的本质(29)——C语言与汇编之寄存器和寻址方式

    x86的通用寄存器有eax.ebx.ecx.edx.edi.esi.这些寄存器在大多数指令中是可以任意选用的,比如movl指令可以把一个立即数传送到eax中,也可传送到ebx中.但也有一些指令规定只能 ...

  6. Search in Rotated Sorted Array I II

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  7. css系列教程--overflow min/maxheight content

    outline:这只轮廓样式,与border类似.写法参考border. overflow/overflow-x/overflow-y:visible/hidden/scroll/auto/no-di ...

  8. AndroidUI--SlidingMenu使用例子

    SlidingMenu简介: SlidingMenu的是一种比较新的设置界面或配置界面效果,在主界面左滑或者右滑出现设置界面,能方便的进行各种操作.目前有大量的应用都在使用这一效果.如Evernote ...

  9. SQL server 数据库基本知识

    SQL server 数据库基本知识 一.数据库: 分为层次型.网状型.关系型.现在通常都是使用关系型 常用的有:SQLserver.Oracle.DB2.Access.Visual Foxpro.M ...

  10. IIS Could not load file or assembly 'CLDBCommon.DLL' or one of its dependencies.找不到指定的模块

    1.卸载原来的.NET4.0,从新下载.NET4.5.1完整安装程序.后问题解决附:.NET4.5.1下载地址:https://www.microsoft.com/zh-cn/download/det ...