LightOJ(1422)——Halloween Costumes

首先,我还是表扬一下自己,开始独立思考了,虽然说最后的想法是错误的,但是至少已经很接近了。所以,再接再厉吧!

题意:

现在那个人要去参加一个聚会,然后总共有n天,每天所要求穿的服饰的序号分别为c[i]。

这个人可以一次性穿上1件衣服,或者一次性脱下任意多件衣服。当然也可以在衣服外面套衣服。

并且如果这件衣服已经被脱下的话,那么它下次不能再次被穿上,如果我们还需要这件衣服的话,那么我们就只能重新再去买另外一件了。

最后问你,如果要参加完所有的派对,那么他所需要的最少的衣服数量是多少。

比如说第一个样例:

4

1 2 1 2

我们只需要先穿上1,然后再穿上2,再脱掉1,然后此时没有2的衣服了,所以我们还需要一件2的衣服,所以最后总共是需要3件衣服。

思路:

这题主要的难点是在于判断什么时候可以利用还存在的衣服。

定义dp[i][j]为i~j区间内所需要的最少衣服的数量。

感觉下面的思路很巧妙:

1)dp[i][i]=1,这是初始化dp

2)dp[i][j]=dp[i][j-1],当c[i]==c[j]的时候,那么我们就只需要j前面的数量就好了

3)当c[i]==c[k]时,dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]); 注意这里是dp[i][k],因为不算k的区间是包含在i~k里的。这里的目的就是相当于再缩小下dp[s][e]

推荐几个好懂的博客:http://www.cnblogs.com/neopenx/p/4050003.html        http://www.cnblogs.com/kuangbin/archive/2013/04/29/3051392.html

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 111
#define inf 99999999
int c[maxn];
int dp[maxn][maxn];
int main(){
	int T,j=1;
	scanf("%d",&T);
	while(T--){
		memset(dp,0,sizeof(dp));
		int n;
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			scanf("%d",&c[i]);
			dp[i][i]=1;
		}
		for(int len=2;len<=n;len++){
			for(int s=1;s<=n-len+1;s++){
				int e=s+len-1;
				dp[s][e]=inf;
				if(c[s]==c[e]) dp[s][e]=dp[s][e-1];
				for(int k=s;k<e;k++){
					if(c[s]==c[k]) dp[s][e]=min(dp[s][e],dp[s][k]+dp[k+1][e]);	//!!!
				}
			}
		}
		printf("Case %d: %d\n",j++,dp[1][n]);
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-08 23:24:35

LightOJ(1422)——Halloween Costumes的相关文章

LightOJ 1422 Halloween Costumes (区间dp 好题)

1422 - Halloween Costumes PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Gappu has a very busy weekend ahead of him. Because, nextweekend is Halloween, and he is planning to attend as many parties as he can.Since it's Hall

LightOJ 1422 Halloween Costumes 区间dp

题意:给你n天需要穿的衣服的样式,每次可以套着穿衣服,脱掉的衣服就不能再穿了,问至少要带多少条衣服才能参加所有宴会 思路:dp[i][j]代表i-j天最少要带的衣服 从后向前dp 区间从大到小 更新dp[i][j]时有两种情况 考虑第i天穿的衣服 1:第i天穿的衣服在之后不再穿了 那么 dp[i][j]=dp[i+1][j]+1; 2:第i天穿的衣服与i+1到j的某一天共用,那么dp[i][j]=min(dp[i][j],dp[i+1][k-1],dp[k][j]),前提是第i天和第k天需要的礼

LightOJ 1422 Halloween Costumes 【区间DP】

题目链接:http://lightoj.com/volume_showproblem.php?problem=1422 解法:dp[i][j]=min(1+dp[i+1][j],dp[i+1][k-1]+dp[k][j]) 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include <complex> #include <str

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

LightOJ 1422 Halloween Costumes

dp[i]][j]=min(dp[i+1][j]+1,dp[i+1][k-1]+dp[k][j]) 表示第i天到j的最小数量.如果第i天的衣服只自己穿的话,不考虑后面的就是dp[i][j]=dp[i+1][j]+1.否则就是dp[i][j]=dp[i+1][k-1]+dp[k][j],其中第k天的衣服和第i天一样,这就是考虑第i天以后还有和它穿的衣服一样的情况 1 //#pragma comment(linker, "/STACK:167772160")//手动扩栈~~~~hdu 用c

LightOJ 1422 Halloween Costumes(记忆化搜索)

题意:给你n天分别要穿的衣服,可以套着穿,但是一旦脱下来就不能再穿了,问这n天要准备几件衣服. ================================================================================= #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> using

LightOJ 1422 Halloween Costumes 【 区间dp 】

区间dp的第一题----- 看题解看了好多~~终于看懂了---55555 dp[i][j] 表示第i天到第j天至少需要多少件衣服 那么第i件衣服只被第i天占用的话, dp[i][j] = dp[i+1][j] + 1 如果不只被第i天占用的话,那么假设在第k天和第i天穿一样的衣服,dp[i][j] = dp[i+1][k-1] + dp[k][j] 这一篇讲得很详细~ http://blog.csdn.net/tc_to_top/article/details/44830317 1 #inclu

Lightoj 题目1422 - Halloween Costumes(区间DP)

1422 - Halloween Costumes   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB 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

light oj 1422 - Halloween Costumes (区间dp)

1422 - Halloween Costumes PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB 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 Ha