关闭

LightOJ 1422 Halloween Costumes (区间dp)

标签: dp
288人阅读 评论(0) 收藏 举报
分类:
1422 - Halloween Costumes
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 Halloween, these parties are all costume parties, Gappu alwaysselects 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 willgo 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 theHalloween night, and wear costumes accordingly, he will be changing hiscostumes a number of times. So, to make things a little easier, he may put oncostumes 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 thecostumes, or wear a new one. That is, if he is wearing the Postman uniform overthe Superman costume, and wants to go to a party in Superman costume, he cantake off the Postman uniform, or he can wear a new Superman uniform. But, keepin 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 theHalloween night, if he needs the Postman costume again, he will have to use anew one. He can take off any number of costumes, and if he takes off kof the costumes, that will be the last k ones (e.g. if he wears costume Abefore costume B, to take off A, first he has to remove B).

Given the parties and the costumes, find the minimum numberof 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 Nintegers, 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 numberof required costumes.

Sample Input

Output for Sample Input

2

4

1 2 1 2

7

1 2 1 1 3 2 1

Case 1: 3

Case 2: 4

 


Problem Setter: Manzurur Rahman Khan
Special Thanks: Jane Alam Jan (Solution, Dataset)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1422
【题意】一个人依次参加n个舞会,每个舞会需要穿的衣服种类为ai,参加完一个舞会后可以脱衣服或者再穿一件衣服或者不做任何操作,注意,脱下的衣服不能再穿,问最少需要多少件衣服。
【思路】区间dp,dp[i][j]表示参加第i-j舞会需要的最少衣服。参加完 第i个舞会后的衣服可以脱掉或者继续穿在身上,若最优状态下是穿在身上的,则说明第i个舞会的衣服重复利用了。只需要枚举第i个舞会后,首次使用第i个舞会的衣服的时间即可。dp[i][j]=min(1+dp[i+1][j](脱掉第i个舞会的衣服),dp[i+1][k-1]+dp[k][j]) num[k]=num[i] i<k<=j;
【代码】
/*************************************************************************
    > File Name: lightoj1422.cpp
    > Author: wanghao
    > Mail: haohaoac@163.com 
    > Created Time: 2015年07月10日 星期五 09时44分39秒
 ************************************************************************/

#include<iostream>
#include<cstring>
#include<cstdio>
#define ll long long
using namespace std;

int dp[110][110];
int num[110];
int main()
{
	int t;
	cin>>t;

		int ca=1;
	while(t--)
	{
		int n;
		cin>>n;
		for(int i=1;i<=n;i++)
			cin>>num[i];
		memset(dp,0,sizeof(dp));
		for(int len=1;len<=n;len++)
			for(int i=1;i+len-1<=n;i++)
			{
				int j=i+len-1;
				if(len==1)dp[i][j]=1;
				else{
					dp[i][j]=1+dp[i+1][j];
					for(int k=i;k<=j;k++)
					{
						if(num[i]==num[k])
							dp[i][j]=min(dp[i][j],dp[i+1][k-1]+dp[k][j]);
					}
				}
			}
		printf("Case %d: %d\n",ca++,dp[1][n]);
	}
	return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:29144次
    • 积分:759
    • 等级:
    • 排名:千里之外
    • 原创:47篇
    • 转载:2篇
    • 译文:0篇
    • 评论:11条
    文章分类
    最新评论