传送门:http://www.lightoj.com/volume_showproblem.php?problem=1422

Halloween Costumes

problem 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 (≤ 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

2

4

1 2 1 2

7

1 2 1 1 3 2 1

Sample Output

Case 1: 3

Case 2: 4


解题心得:

  1. 题意就是一个人要去参见很多个化装舞会,每个化妆舞会要穿特定的衣服,那人可以一开始穿很多件衣服在身上,也可以脱下衣服,但是脱下之后的衣服不能再穿,问这个人要参加所有的化装舞会做少需要用多少衣服。
  2. 其实就是一个区间dp,dp[i][k]代表从第i个化妆舞会到第j个化妆舞会需要得最少衣服,因为可以穿多件衣服所以状态转移方程式很容易就推出来了:
    • 如果第j个化妆舞会和第i个不同,dp[i][k]=dp[i][k-1]+1;

      如果相同则dp[i][k] = dp[i][k-1];
    • 然后枚举从i到j区间里面的每种匹配情况,然后更新出最少衣服的情况。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 110;
int dp[maxn][maxn],num[maxn];
int main()
{
int t;
scanf("%d",&t);
int T = 1;
while(t--)
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&num[i]);
dp[i][i] = 1;
} for(int i=1;i<n;i++)
{
for(int j=0,k=i;k<n;k++,j++)
{
if(num[j] != num[k])
dp[j][k] = dp[j][k-1] + 1;
else
dp[j][k] = dp[j][k-1];
for(int z=j;z<k;z++)
if(dp[j][k] > dp[j][z] + dp[z+1][k])
dp[j][k] = dp[j][z] + dp[z+1][k];
}
}
printf("Case %d: ",T++);
printf("%d\n",dp[0][n-1]);
}
return 0;
}

最新文章

  1. 编译链接 C++
  2. SPSS数据分析—典型相关分析
  3. Java内存分配及变量存储位置实例讲解
  4. 描述Linux运行级别的0-6的各自含义(计时1分钟)
  5. paip.编程语言方法重载实现的原理及python,php,js中实现方法重载
  6. Java-transient
  7. AngularJs 入门系列-1 使用 AngularJs 搭建页面基本框架
  8. 【ILSpy反编译】C# 写的程序反编译查看是不是也太容易了点吧,太恐怖了。。。
  9. 提供几个可注册的edu邮箱链接
  10. Hadoop动态加入/删除节点(datanode和tacktracker)
  11. 访问Ice-Pick Lodge:假设公众筹款网站Kickstarter在成功
  12. jquery选择器之基本筛选选择
  13. Eclipse插件springsource-tool-suite在线和离线安装步骤
  14. http://acm.hdu.edu.cn/showproblem.php?pid=1039(水~)
  15. PHP多进程消费队列
  16. java框架之SpringBoot(17)-监控管理
  17. Vue(六) 表单与 v-model
  18. MySQL Crash Course #08# Chapter 16. Using Different Join Types
  19. springboot跨域请求
  20. ping命令技巧详解 windows下ping命令知识大全

热门文章

  1. Golang 入门系列(十三)用Beego开发web应用
  2. PHP的时间函数strtotime
  3. vfp使用笔记
  4. webpack.config.js====插件purifycss-webpack,提炼css文件
  5. zTree的重点
  6. (五)我的JavaScript系列:JavaScript的糟粕
  7. 【Python图像特征的音乐序列生成】数据集制作的一些tricks
  8. 【Python图像特征的音乐序列生成】关于音乐生成的思路转变
  9. 如何使用ABAP Restful API进行代码的全文搜索
  10. cesium-大规模人群运动测试