# LightOJ 1422 Halloween Costumes (区间dp)

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.

# 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)

【题意】一个人依次参加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;
}

• 本文已收录于以下专栏：

## LightOJ 1422 Halloween Costumes 区间DP

• hcbbt
• 2013年11月11日 22:43
• 2743

## LightOJ1422---Halloween Costumes (区间dp)

Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and he is planning t...

## LightOJ - 1422Halloween Costumes （区间DP）

Halloween CostumesGappu has a very busy weekend ahead of him. Because, next weekend is Halloween, an...

## LightOj 1422 Halloween Costumes（区间DP）

LightOj 1422 Halloween Costumes（区间DP）
• Dacc123
• 2016年03月26日 10:47
• 210

## LightOJ 1422 Halloween Costumes（区间DP ）

Description Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and h...

## lightoj 1422Halloween Costumes（区间dp）

dp[i][j]代表从区间i到区间j最少的穿衣数量，那么在dp[i][j]这个状态的穿衣数，就要等于dp[i+1][j]+1；也就是说，首先在不考虑它后面是否有一天要穿相同的衣服的情况下，它肯定会比区...

## Lightoj 1422 Halloween Costumes(区间DP)

1422 - Halloween Costumes PDF (English) Statistics Forum Time Limit: 2...
• fp_hzq
• 2012年10月05日 21:14
• 1383

## LightOJ 1422 Halloween Costumes（区间dp）

Halloween Costumes Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

## Light oj 1422 Halloween Costumes（区间DP:迭代or记忆化搜索）

举报原因： 您举报文章：LightOJ 1422 Halloween Costumes (区间dp) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)