LightOj 1422 Halloween Costumes(区间DP)
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)的更多相关文章
- LightOJ - 1422 Halloween Costumes —— 区间DP
题目链接:https://vjudge.net/problem/LightOJ-1422 1422 - Halloween Costumes PDF (English) Statistics F ...
- LightOJ 1422 Halloween Costumes 区间dp
题意:给你n天需要穿的衣服的样式,每次可以套着穿衣服,脱掉的衣服就不能再穿了,问至少要带多少条衣服才能参加所有宴会 思路:dp[i][j]代表i-j天最少要带的衣服 从后向前dp 区间从大到小 更新d ...
- light oj 1422 Halloween Costumes (区间dp)
题目链接:http://vjudge.net/contest/141291#problem/D 题意:有n个地方,每个地方要穿一种衣服,衣服可以嵌套穿,一旦脱下的衣服不能再穿,除非穿同样的一件新的,问 ...
- Light OJ 1422 - Halloween Costumes(区间DP 最少穿几件)
http://www.cnblogs.com/kuangbin/archive/2013/04/29/3051392.html http://www.cnblogs.com/ziyi--caolu/a ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
- LightOJ - 1422 Halloween Costumes (区间dp)
Description Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and he i ...
- LightOJ 1422 Halloween Costumes 【 区间dp 】
区间dp的第一题----- 看题解看了好多~~终于看懂了---55555 dp[i][j] 表示第i天到第j天至少需要多少件衣服 那么第i件衣服只被第i天占用的话, dp[i][j] = dp[i+1 ...
- LightOJ 1422 Halloween Costumes (区间DP,经典)
题意: 有个人要去参加万圣节趴,但是每到一个趴都要换上特定的服装,给定一个序列表示此人要穿的衣服编号(有先后顺序的),他可以套很多件衣服在身上,但此人不喜欢再穿那些脱下的衣服(即脱下后就必须换新的), ...
- 【LightOJ 1422】Halloween Costumes(区间DP)
题 题意 告诉我们每天要穿第几号衣服,规定可以套好多衣服,所以每天可以套上一件新的该号衣服,也可以脱掉一直到该号衣服在最外面.求最少需要几件衣服. 分析 DP,dp[i][j]表示第i天到第j天不脱第 ...
随机推荐
- e671. 在缓冲图像中存取像素
// Get a pixel int rgb = bufferedImage.getRGB(x, y); // Get all the pixels int w = bufferedImage.get ...
- C++ 中的空格
C++ 中的空格只包含空格的行,被称为空白行,可能带有注释,C++ 编译器会完全忽略它. 在 C++ 中,空格用于描述空白符.制表符.换行符和注释.空格分隔语句的各个部分,让编译器能识别语句中的某个元 ...
- taskAffinity属性
Activity的归属,也就是Activity应该在哪个Task中,Activity与Task的吸附关系.我们知道,一般情况下在同一个应用中,启动的Activity都在同一个Task中,它们在该Tas ...
- R语言boxplot绘图函数
boxplot 用于绘制箱线图,我们都知道boxplot 用于展示一组数据的总体分布,在R语言中,支持两种输入数据的方式 第一种:x , 这个参数指定用于绘制箱线图所用的数据,是一个向量 代码示例: ...
- React中props与state
以下内容均为个人理解. 1.state: 在react中,state可以看成管理页面状态的集合(实则一个对象而已),库里面的成员均为页面渲染变量,整个页面为一个状态机,当state发生变化时,页面会重 ...
- php通过字符串生存hashCode
/** * * 生存hashCode * */function hashCode($str){ if(empty($str)) return ''; $str = strtoupper($str); ...
- 什么是代码?code?
概念描述: 程序代码?code? 应用程序是由一系列代码构成,那么什么是代码呢? 简单来说:代码也可以理解为命令,通过这个命令告诉计算机该做什么事情. 文档创建时间:2018年3月16日15:10:5 ...
- Spring MVC异常统一处理
package com.shzq.common.exception; import java.io.PrintWriter;import java.io.StringWriter;import jav ...
- UVa 10450 - World Cup Noise
题目:构造一个01串,使得当中的1不相邻,问长度为n的串有多少中. 分析:数学,递推数列. 设长度为n的串有n个.则有递推关系:f(n)= f(n-1)+ f(n-2): 长度为n的结束可能是0或者1 ...
- day23<File类递归练习>
File类递归练习(统计该文件夹大小) File类递归练习(删除该文件夹) File类递归练习(拷贝) File类递归练习(按层级打印) 递归练习(斐波那契数列) 递归练习(1000的阶乘所有零和尾部 ...