LightOJ - 1422 Halloween Costumes (区间dp)
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 (≤ ), denoting the number of test cases. Each case starts with a line containing an integer N( ≤ N ≤ ) denoting the number of parties. Next line contains N integers, where the ith integer ci( ≤ ci ≤ ) denotes the costume he will be wearing in party i. He will attend party first, then party , and so on.
Output
For each case, print the case number and the minimum number of required costumes.
Sample Input
Sample Output
Case : Case :
题意:有N个宴会,对于每一个宴会,女猪脚都要穿一种礼服,礼服可以套着穿,但是脱了的不能再用,参加宴会必须按顺序来,从第一个到第N个,问参加这些宴会最少需要几件礼服
比如说第一个样例:第一天穿衣服“1”,然后不脱,在第二天穿上第二件衣服“2”,第三天脱掉衣服“2”,第四天穿上新的衣服“2”,总共要3件衣服。
思路:区间dp,从后往前推
首先每增加一天要增加一件衣服,dp[i][j]=dp[i+1][j]+1.
然后k在i+1~j的区间,若a[i]==a[k]说明衣服可以不用脱下来,可以穿着在k天的时候再穿,所以只需要选一件即可。所有转移方程为:dp[i][j]=min(dp[i][j],dp[i+1][k-1]+dp[k][j])
再看边界问题:有两种初始化的方法:
1、for(int i=1;i<=n;i++){
dp[i][i]=1;
}即每一天需要一件衣服
2、区间的DP值 可以设为 i,j的区间长度即可,
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
dp[i][j] = j - i + 1;
AC代码:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 106
#define inf 1e12
int n;
int a[N],dp[N][N];
int main()
{
int t;
int ac=;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
} for(int i=;i<=n;i++){
dp[i][i]=;
}
for(int len=;len<n;len++){
for(int i=;i+len<=n;i++){
int j=i+len;
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]);
}
}
}
}
printf("Case %d: ",++ac);
printf("%d\n",dp[][n]);
}
return ;
}
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 ...
- LightOj 1422 Halloween Costumes(区间DP)
B - Halloween Costumes Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
- 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天不脱第 ...
随机推荐
- DPark安装及相关资料整理
最近需要处理海量数据的分布式计算及数据挖掘,经过多次选择(hadoop,Spark,DPark),最后还是选择了DPark,主要是看中DPark的轻量级及python的灵活性,且除了豆瓣外,在几个友公 ...
- ubuntu 10.10配置NFS网络共享步骤
安装好交叉编译环境arm-linux-gcc之后,就开始配置NFS网络共享,用于不同的linux主机与目标机之间文件的共享.如果是windows和linux的共享则需用samba服务. NFS(Net ...
- hdu 1166 敌兵布阵_线段树
题意:略 思路:这题是单点更新,如果是减少的话,直接把数据变成负加上去就行了. #include <iostream> #include<cstdio> #include< ...
- SpringNote01.基于SpringMVC-Hibernate的Blog系统
最近,在学习Spring,做这样一个简单的blog系统,主要是让自己动手练习使用Spring,熟练的使用才干进一步的深入学习.该项目使用Maven构建,使用git进行代码管理,通过这样一个小项目,熟悉 ...
- 11gR2 RAC启用iptables导致节点宕机问题处理
通常,在安装数据库时,绝大多数都是要求把selinux及iptables关闭,然后再进行安装的.但是在运营商的系统中,很多安全的因素,需要将现网的数据库主机上的iptables开启的. 在开启ipta ...
- animate CSS动画程序接口(仅Chrome可用)
jQuery中很早就提供了animate方法,使用它可以很方便地实现一些简单动画效果.后来CSS3中也提供了animation用于动画效果制作,但CSS本身的可操作性太差,所以用起来并不方便.现在最新 ...
- HTML5新特性学习-2
本文在于巩固基础 HTML5绘图基础 <canvas>画布元素的使用 <div> <canvas id="can" width="200px ...
- js判断手机的类型
var ua = navigator.userAgent;if(ua.match(/iPhone|iPod/i) != null){console.log("iphone代码"); ...
- 数据库基础(变量、运算符、if语句、while语句)
数据库基础(变量.运算符.if语句.while语句) 变量: 定义变量:declare @变量名 数据类型 变量赋值:set @变量名 = 值 输出:print 变量或字符串 SQL语言也跟其他编 ...
- There is no satiety in study
好不容易考上了硕士.这个时候,才终于明白什么叫做学无止境.用了1周linux,发现需要学习的东西太多了.life is too short to learn c plus plus 果然如此.不过我们 ...