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. tomcat------https单向认证和双向认证

     一.https分为单向认证和双向认证: 单向认证就是说,只有客户端使用ssl时对服务器端的证书进行认证,也就是说,客户端在请求建立之前,服务器端会向客户端发送一个证书,一般情况下,这种证书都是由自己 ...

  2. Amazon 解决下载文件乱码

    大家在做多个站点的时候,可能会遇到下载下来的报告文件出现乱码. 法国站点和意大利站点均会出现这样的情况,那怎么解决呢? 这是由于编码的问题而导致,在我们读取数据插入到本地数据库的时候,不妨先将格式转成 ...

  3. IPicture总结

    1.利用IPicture接口加载.显示图片 IPicture接口管理一个图片对象和它的属性.图片对象提供对Bitmap Icon Metafile的语言不相关的抽象支持.图像对象的主要接口是IPict ...

  4. 【BZOJ 1088 扫雷Mine】模拟

    http://www.lydsy.com/JudgeOnline/problem.php?id=1088 2*N的扫雷棋盘,第二列的值a[i]记录第 i 个格子和它8连通的格子里面雷的数目. 第一列的 ...

  5. 关于 .crash 分析

    这里只给出其中 一种方式. 1. 建议 桌面 建 个文件夹  appxx  ,然后 将那个闪退 对应的 包  xxx.app 放入  appxx文件夹 2. 打开终端cd命令,进入该文件夹 3.在命令 ...

  6. iOS 四种延时的方法

    - (void)initBlock{     //延时的方法     //1:GCD延时 此方式在能够在參数中选择运行的线程. 是一种非堵塞的运行方式,没有找到取消运行的方法.     double ...

  7. android 实现自己定义状态栏通知(Status Notification)

    在android项目的开发中,有时为了实现和用户更好的交互,在通知栏这一小小的旮旯里,我们通常须要将内容丰富起来,这个时候我们就须要去实现自己定义的通知栏,比如以下360或者网易的样式: 首先我们要了 ...

  8. oracle优化思考-双刃剑

    oracle优化是一个双刃剑,特别注意这把剑用的场合:系统规划OLTP or OLAP 优化1:索引 在DML操作时.必须维护索引.假设大量的DML操作,想想看,IO是不是老高了? 索引长处:在非常多 ...

  9. DoNet开源项目-基于Amaze UI的点餐系统

    帮朋友做的点餐系统,主要是为了让顾客在餐桌上,使用微信扫描二维码,就可以直接点菜,吃完使用微信付款. 系统演示地址,账户名和密码均为:admin.(请不要删除admin用户) GitHub Clone ...

  10. git clone 命令报错 +diffie-hellman-group1-sha1

    解决方法: 在.ssh目录下新建文件config , 添加 Host * KexAlgorithms +diffie-hellman-group1-sha1 到文件config,即可.