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. e661. 确定图像中是否有透明像素

    // This method returns true if the specified image has transparent pixels public static boolean hasA ...

  2. 说说自己对hibernate一级、二级、查询、缓存的理解。

    说说自己对hibernate一级.二级.查询.缓存的理解. 2016-03-14 21:36 421人阅读 评论(0) 收藏 举报  分类: web开发(19)  版权声明:本文为博主原创文章,未经博 ...

  3. AngularJS开发经验

    AngularJS是为了克服HTML在构建应用上的不足而设计的.HTML是一门很好的为静态文本展示设计的声明式语言,但要构建WEB应用的话它就显得乏力了.所以我做了一些工作(你也可以觉得是小花招)来让 ...

  4. 当点击onBackpress方法或者onKeyDown方法时出现不能响应的问题解决办法

    一般来讲,当我们点击手机上的back键之后,会直接调用activity的onbackpress()方法,或者我们也可以通过这样来进行捕捉点击事件 @Override public boolean on ...

  5. par函数col参数-控制颜色

    col参数用来控制颜色,其实有一些列的颜色相关的参数,都是以col 开头 col : 控制图片中点,文字以及绘图边框的颜色,代码示例: par(col = "red") plot( ...

  6. CentOS基础命令大全

    1.关机 (系统的关机.重启以及登出 ) 的命令 shutdown -h now 关闭系统(1) init 0 关闭系统(2) telinit 0 关闭系统(3) shutdown -h hours: ...

  7. Asp.net mvc中应用autofac

    1.nuget安装依赖

  8. 【Java面试题】13 Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以implements(实现)interface(接口)?

    1.什么是匿名内部类? 内部类,存在于另一个类内部的类,而匿名内部类,顾名思义,就是没有名字的内部类. 2.为什么需要匿名内部类? 每个inner class都能够各自继承某一实现类(implemen ...

  9. ActiveMQ-5.13.0集群

    ActiveMQ集群介绍 ActiveMQ具有强大和灵活的集群功能,但在使用的过程中会发现很多的缺点,ActiveMQ的集群方式主要由两种:Master-Slave(ActiveMQ5.8版本已不可用 ...

  10. PHPCMS v9设置文章的审核功能

    对于新建的站点,如果想设置会员发布的文章必须通过审核后才能发布,则需要以下几步来完成: 1.根据需要自定义管理员角色或选择已有角色. 步骤:设置->管理员设置->角色管理->权限设置 ...