您的位置:首页 > 其它

1422 - Halloween Costumes(区间DP)

2015-10-10 20:51 288 查看
Link:http://www.lightoj.com/volume_showproblem.php?problem=1422

1422 - Halloween Costumes



PDF (English)StatisticsForum
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 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 (≤ 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 N integers, 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 number of 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)

题意:给出每天需要穿的衣服种类,每穿一件衣服次数加一,穿上的衣服可以脱下,但再穿这件衣服时次数加要一,衣服也可以穿在里面,当需要用到该类衣服时脱掉外面的即可,这时次数不变,求满足题目要求的穿衣顺序时需要穿衣服的最小次数。

样例[b]解释

第一个样例,先穿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各一件。
[/b]



编程思想:区间DP。dp[i][j]表示在第i天至第j天穿衣的最少次数。当从第一天开始,从前往后推时(阶段),由于后面的穿衣次数(状态和决策)受到前面的影响(由于当前的最优状态无法由前面穿衣的状态推出来,因为前面穿衣的状态复杂,无法表示,故这种递推方式无法得到结果),故不能这样动态规划,必须改为从后往前推,因为后面的状态(已经是前面状态导致的结果了)不会影响到当前的决策,反过来,当前的最优状态很容易由后面的(最优子状态)结果推出(因为后面的最优结果确定时,前面每一步的过程状态对应的也应该是最优的且确定了,即有可能有不同的过程决定最优结果,但最优结果至少可逆推出一个可能的过程,因为该结果是最优的,逆推回去也是最优的,这也是自顶向下的思想),为什么从后面往前面推呢,我感觉就像是01背包为什么从后面往前面推一样,防止前面的影响后面的,所以我们这里进行倒着for。所以逆推过程可得出状态转移方程为:

状态转移:

1:dp[i][j] = dp[i+1][j],区间增加一个先直接判断需要多一件,然后进入第二状态的来判定。

2:dp[i][j] = min(dp[i][j],dp[i+1][k-1] + dp[k][j]),这个是 第i天 需要的礼服与第k天需要的礼服一样,所以只需要选一件即可,所以后面找的 是 dp[i+1][k-1]+dp[k][j],来判断到底哪个小。




AC code:



#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int dp[111][111];
int type[111];
int main(){
int T,n,i,j,k,cas,len;
scanf("%d",&T);
for(cas=1;cas<=T;cas++)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&type[i]);
}
memset(dp,0,sizeof(dp));
for(i=1;i<=n;i++)
{
dp[i][i]=1;
}
for(i=n-1;i>=1;i--)//枚举起点
{
for(j=i+1;j<=n;j++)//枚举终点
{
dp[i][j]=dp[i+1][j]+1;
for(k=i+1;k<=j;k++)//i+1<=k<=j判断是否存在type[k]==type[i]
{
if(type[k]==type[i])
dp[i][j]=min(dp[i][j],dp[i+1][k-1]+dp[k][j]);
}
}
}
printf("Case %d: %d\n",cas,dp[1]
);
}
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: