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 Nintegers, 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

2

4

1 2 1 2

7

1 2 1 1 3 2 1

Sample Output

Case 1: 3

Case 2: 4

题目大意:

给定一串数字,代表要参加聚会的顺序以及编号,每个聚会只能穿指定的一件衣服,可以在衣服外面套衣服,也可以脱衣服,

若脱了这件衣服就不能再穿了,求最小需要多少件衣服。

dp[i][j]代表区间i到j需要的最小件数,假设i+1到j没有和i相同的时候,dp[i][j]=dp[i+1][j]+1,然后循环区间i+1到j,若相同就更新。

#include <iostream>
#include <algorithm>
using namespace std;
int a[],dp[][];
int main()
{
ios::sync_with_stdio(false);
int T,o=;
cin>>T;
while(T--)
{
int n;
cin>>n;
for(int i=;i<=n;i++)
cin>>a[i],dp[i][i]=;
for(int i=n;i>;i--)
for(int j=i+;j<=n;j++)
{
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]);
}
cout<<"Case "<<++o<<": "<<dp[][n]<<'\n';
}
return ;
}

Halloween Costumes(区间DP)的更多相关文章

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

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

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

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

  3. LightOJ 1422 Halloween Costumes 区间dp

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

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

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

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

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

  6. UVA 4857 Halloween Costumes 区间背包

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  7. 区间DP小结

    也写了好几天的区间DP了,这里稍微总结一下(感觉还是不怎么会啊!). 但是多多少少也有了点感悟: 一.在有了一点思路之后,一定要先确定好dp数组的含义,不要模糊不清地就去写状态转移方程. 二.还么想好 ...

  8. LightOJ - 1422 Halloween Costumes (区间dp)

    Description Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and he i ...

  9. LightOj 1422 Halloween Costumes(区间DP)

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

  10. Lightoj 题目1422 - Halloween Costumes(区间DP)

    1422 - Halloween Costumes   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 ...

随机推荐

  1. RHEL6.5---LVS(IP-TUN)

    实验环境: 主机名 IP  所需软件 master eth0==>192.168.30.160(RIP) eth0:1==>192.168.30.130(VIP) ipvsadm node ...

  2. Git之提交项目到远程github

    1.在分支dev下,默认本地工作区有项目project 2. git add project  [添加项目到暂存区] 3. git commit project -m "提交项目" ...

  3. sql server 2012 从删库到跑路

    问题: 向sql server 2012单个数据库中导入1500万+条数据的时候,报错: 错误 0xc0202009: 数据流任务 1: SSIS 错误代码 DTS_E_OLEDBERROR.出现 O ...

  4. vue中的事件监听之——v-on vs .$on

    跟着视频中老师的教学视频学vue的时候,看很多时候都用@(v-on)来监听子级emit的自定义事件,但在bus总线那块,又用.$on来监听bus自身emit的事件,v-on之间似乎相似但又不同,今天对 ...

  5. http://bbs.chinaunix.net/thread-1463276-1-1.html

    http://bbs.chinaunix.net/thread-1463276-1-1.html

  6. XSS漏洞扫描经验分享

    关于XSS漏洞扫描,现成的工具有不少,例如paros.Acunetix等等,最近一个项目用扫描工具没有扫出漏洞,但还是被合作方找出了几个漏洞.对方找出的漏洞位置是一些通过javascript.ajax ...

  7. 关于maven source1.5报错

    是因为maven 默认是1.5编译的 <build>//加上这个配置,把编译给改掉试试 <pluginManagement> <plugins> <plugi ...

  8. 洛谷 U10206 Cx的治疗

    题目背景 「Cx的故事」众所周知,Cx是一个宇宙大犇.由于Cx在空中花园失足摔下,导致他那蕴含着无穷智慧的大脑受到了严重的损伤,许多的脑神经断裂.于是,Cx的wife(有么?)决定请巴比伦最好的医师治 ...

  9. Java实现Web页面前数字字母验证码实现

    最近公司做项目开发中用到了验证码实现功能,将实现代码分享出来, 前段页面实现代码: 为了表达清晰,样式部分代码去掉了,大家根据自己的需求,自己添加样式. 页面JS代码:触发变动验证码改变的JS 后台 ...

  10. 1.1 NLP基础技能,字符串的处理

    #!/usr/bin/env python # coding: utf-8 # # 字符串操作 # ### 去空格和特殊字符 # In[8]: s = " hello world! &quo ...