LightOJ 1422 Halloween Costumes (区间dp 好题)

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 offkof the costumes, that will be the lastk ones (e.g. if he wears costumeAbefore costumeB, to take off A, first he has to removeB).

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 integerN (1≤ N ≤ 100) denoting the number of parties. Next line containsNintegers, where theith integer ci (1 ≤ci ≤ 100) denotes the costume he will be wearing in partyi.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 InputOutput for Sample Input

2

4

1 2 1 2

7

1 2 1 1 3 2 1

Case 1: 3

Case 2: 4

题目链接:?problem=1422

题目大意:有n个地方,每个地方要穿一种衣服,衣服可以嵌套穿,一旦脱下的衣服不能再穿,除非穿同样的一件新的,问在满足题目要求的穿衣顺序下最少需要准备几件衣服

题目分析:先解释一下样例

第一个样例,先穿1,,再脱1穿2,然后不脱2,直接再穿1,再脱2,一共3件,2件1,1件2即可

第二个样例,先穿1,再不脱1穿2,再不脱2穿1(此时为121),再脱1穿3(此时为321)然后直接脱就行了,一共4件,2件1,2和3各一件

下面说思路,dp[i][j]表示第i个地方到第j个地方最少需要准备几件衣服,初始化dp[i][j](i <= j)为1,因为至少要1件,对于第i个地方,我们先假设其状态与后面无关即需要穿它,则dp[i][j] = dp[i+1][j] + 1,然后再从i+1向后枚举k,若c[i] == c[k],则表示之后有一个地方要穿的与第i个地方相同,则我们可以不脱它,因为第i天和第k天穿的相同,将区间分成两段,表示为dp[i][j] = dp[i+1][k – 1] + dp[k][j]

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int dp[105][105], c[105];int main(){int T;scanf("%d", &T);for(int ca = 1; ca <= T; ca++){int n;scanf("%d", &n);for(int i = 1; i <= n; i++)scanf("%d", &c[i]);memset(dp, 0, sizeof(dp));for(int i = 1; i <= n; i++)for(int j = i; j <= n; j++)dp[i][j] = 1; //第i天到第j天至少准备1件for(int i = n – 1; i >= 1; i–) //从第n-1天向前枚举{for(int j = i; j <= n; j++) //从第i天向后枚举{dp[i][j] = dp[i + 1][j] + 1; //第i天需要准备for(int k = i + 1; k <= j; k++) //从第i+1天向后枚举if(c[i] == c[k]) //若第k天与第i天衣服相同//则将区间分成两段,相当于在第i天我尝试不脱下那件衣服而是穿第i+1天的衣服//去覆盖它,到第k天我把前面的都脱了,将i天的衣服展示出来dp[i][j] = min(dp[i][j], dp[i + 1][k – 1] + dp[k][j]);}}printf("Case %d: %d\n", ca, dp[1][n]);}}

当你困难失望的时候,最重要的是事瞧得起你自己;

LightOJ 1422 Halloween Costumes (区间dp 好题)

相关文章:

你感兴趣的文章:

标签云: