B - Halloween Costumes

Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu

Submit

Status

Practice

LightOJ 1422

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

FAQ | About Virtual Judge | Forum | Discuss | Open Sourc

给一个问题

目标字符串 aaabbccaaa

一次只能在固定的区间内,将区间内的字符全都变成相同的字符。问你从一个空字符串到目标字符串最少几次可以变成。

解决方法和这道题目的解法是如出一辙。其实二者是完全一样的问题,一件穿了多少天就相当于在一个固定的区间覆盖了一个字符。

状态转移方程:

dp[i][j]=dp[i+1][j]+1;

dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k+1][j]).{k=i+1…j}

做动态规划的题目应该经常思考状态转移方程式怎么得来的。状态方程的意思理解还远远不够。

联系题目,最坏的结果是每遇到一天都新穿一件衣服。那么最优的情况是怎么得来的,自然是穿上一件衣服不用脱,因为在后来可以派上用场,这样就少新买一件衣服。

我们可以从区间的左端或者右端开始,和区间里的数比较,相等的话就划分区间。

这样的话可以得到全局最优解,注意递推的时候,一定要使子状态都计算过了。

关于区间DP,可以参照这个博客

http://blog.csdn.net/dacc123/article/details/50885903

从左端开始比较:

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h> using namespace std;
int n;
int a[105];
int dp[105][105];
int main()
{
int t;
scanf("%d",&t);
int cas=0;
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
memset(dp,0,sizeof(dp)); for(int i=n;i>=1;i--)
{
for(int j=i;j<=n;j++)
{
dp[i][j]=dp[i+1][j]+1;
for(int k=i+1;k<=j;k++)
{
if(a[i]==a[k])
dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k+1][j]);
}
}
}
printf("Case %d: %d\n",++cas,dp[1][n]); }
return 0;
}

从右端开始比较

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h> using namespace std;
int n;
int a[105];
int dp[105][105];
int main()
{
int t;
scanf("%d",&t);
int cas=0;
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
memset(dp,0,sizeof(dp)); for(int l=0;l<n;l++)
{
for(int i=1;i+l<=n;i++)
{
int j=i+l;
dp[i][j]=dp[i][j-1]+1;
for(int k=i;k<=j-1;k++)
{
if(a[j]==a[k])
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j-1]);
}
}
}
printf("Case %d: %d\n",++cas,dp[1][n]); }
return 0;
}

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. 区间DP LightOJ 1422 Halloween Costumes

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

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

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

  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. 第三百一十六节,Django框架,中间件

    第三百一十六节,Django框架,中间件 django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间 ...

  2. php将汉字转换为拼音和得到词语首字母(二)

    <?php class Pinyin{ private $_outEncoding = "GB2312"; public function getPinyin($str,$p ...

  3. 关于android 调用网页隐藏地址栏

    首先创建项目,在main.xml里 添加好WebView控件R.id为webview1. HelloWebView.java 代码 package liu.ming.com; import andro ...

  4. Unity教程之-基于行为树与状态机的游戏AI

    AI.我们的第一印象可能是机器人,现在主要说在游戏中的应用.关于AI的相关文章我们在前面也提到过,详细请戳这现代的计算机游戏中已经大量融入了AI元素,平时我们进行游戏时产生的交互都是由AI来完成的.比 ...

  5. UI设计要学哪些软件

    准备做UI设计的或是已经在做UI设计的童鞋,哪些软件是我们要学习的重点,作者把UI设计分成了好几个不同的职业方向,从事什么UI设计方向,就学什么软件,这样针对性就很强了,无论怎么说,Photoshop ...

  6. Shell脚本中$0、$?、$!、$$、$*、$#、$@等的意义

    http://blog.csdn.net/slovyz/article/details/47400107

  7. python--list和tuple类型--2

    原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 一.创建list Python内置的一种数据类型是列表:list.list是一种有序的集合,可以 ...

  8. Effective C++ Item 12 Copy all parts of an object

    This one is simple, do not forget to copy all parts of an object in copy constructor or assignment o ...

  9. cmp()

    cmp(x, y) 用于比较两个对象的大小,如果 x > y 返回 1 ,如果 x = y 返回 0 ,如果 x < y 返回 -1 In [23]: cmp(5, 2) Out[23]: ...

  10. osgEarth2.8加载矢量数据描边效果

    通过修改osgearth自带的agglite插件,实现矢量描边效果,可以自定义描边的颜色和宽度(单位像素) 测试文件osgearth_features.cpp #include <osg/Not ...