LightOJ 1422 Halloween Costumes (区间DP OR 记忆化搜索 方法总结)

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

2

4

1 2 1 2

7

1 2 1 1 3 2 1

Sample Output

Case 1: 3

Case 2: 4


感觉看这个题之前先看这个也行:here


题意:

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

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

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

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

比如说第一个样例:

4

1 2 1 2

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

思路:

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

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

以上参考网址:here

想一想为什么是逆向DP的,原因很简单,就是因为如果正向DP的话还有子问题是不知道的
比如在dp状态表达的含义相同的前提下,这道题里面如果正向表达式就是dp[i][j]=min(1+dp[i][j-1],dp[i][k-1]+dp[k][j-1])   (    a[i]==a[k]   i<k<=j-1   )
这样的话dp[k][j-1]是没有求出来的,所以需要逆向


从这道题里面能学到的东西很明显,就是在分析完最优子结构、确定状态并写出递推方程的时候,如果发现递推方程里面有不知道的状态,
不放就用这种逆向的方式解决问题。


记忆化搜索和DP之间的联系:

记忆化搜索和DP的状态是一样的,所以其本质还是需要分析出最优子结构(当然这道题完全时可以一用记忆化搜索的,以后有时间补上)

dp问题的步骤
(1)分析最优子结构,分割子问题,这里子问题都是有特点的,比如串,可能就是和某个其他字符匹配,因为就可以从匹配的位置分割子问题
(2)确定dp状态的含义
(3)得到递推方程式(其中步骤三和步骤二可能会重叠想)
所以以后再写DP的问题,先按照上面的步骤解题,题解报告也都按照这个步骤写!!!

第一次正式地做区间DP,有点啰嗦了!!!


/*
 * Light OJ 1422 - Halloween Costumes
 * http://lightoj.com/volume_showproblem.php?problem=1422
 *  区间DP的思想。  
 *  比如要求解i到j,dp[i][j].
 *  就是考虑i的衣服,一种是i的衣服只有在i使用,那么就是dp[i+1][j]+1件
 *  然后再i+1~j中枚举k,如果a[i]==a[k].那么dp[i][j]=min(dp[i][j],dp[i+1][k-1]+dp[k][j])
 *  注意因为i的衣服是可以使用多次的,所以不需要加1,是dp[k][j]
 *  思想很妙
  
    很简单的区间DP的入门题。一开始这题想了很久就是想不出来。直到做了后面几道区间DP回过来终于想明白了。
    区间DP可以使用记忆化搜索和直接DP的方法写。
    这题的状态转移方程:这个是看的第i件衣服,有点抽象  dp[i][j]=min(1+dp[i+1][j],dp[i+1][k-1]+dp[k][j])   (    a[i]==a[k]   i<k<=j   )
     注意初始化。                                      穿上一件衣服    脱掉k到j件衣服
 */


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
const int MAXN=110;
int a[MAXN];
int dp[MAXN][MAXN];


int main()
{
    int T;
    int n;
    scanf("%d",&T);
    int iCase=0;
    while(T--)
    {
        iCase++;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
            dp[i][i]=1;
        for(int i=n-1;i>=1;i--)///注意DP的顺序
            for(int j=i+1;j<=n;j++)
            {
                dp[i][j]=dp[i+1][j]+1;///这个表示第i个的衣服在后面没有利用了
                for(int k=i;k<=j;k++)
                    if(a[i]==a[k])///用同一件
                        dp[i][j]=min(dp[i][j],dp[i+1][k-1]+dp[k][j]); ///把i+1到k-1件衣服脱掉
            }
        printf("Case %d: %d
",iCase,dp[1][n]);
    }
    return 0;
}



这个是看的第j件衣服!!!好想一些,因为看到是最后一件

/*
 * Light OJ 1422 - Halloween Costumes
 * http://lightoj.com/volume_showproblem.php?problem=1422
 *  区间DP的思想。
 *  比如要求解i到j,dp[i][j].
 *  就是考虑i的衣服,一种是i的衣服只有在i使用,那么就是dp[i+1][j]+1件
 *  然后再i+1~j中枚举k,如果a[i]==a[k].那么dp[i][j]=min(dp[i][j],dp[i+1][k-1]+dp[k][j])
 *  注意因为i的衣服是可以使用多次的,所以不需要加1,是dp[k][j]
 *  思想很妙
 */

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
const int MAXN=110;
int a[MAXN];
int dp[MAXN][MAXN];

int main()
{
    int T;
    int n;
    scanf("%d",&T);
    int iCase=0;
    while(T--)
    {
        iCase++;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
            for(int j=i;j<=n;j++)
                dp[i][j]=j-i+1;
        for(int i=n-1;i>=1;i--)//注意DP的顺序
            for(int j=i+1;j<=n;j++)
            {
                dp[i][j]=dp[i][j - 1]+1;//这个表示第i个的衣服在后面没有利用了
                for(int k=i;k<=j - 1;k++)
                    if(a[j]==a[k])//用同一件
                        dp[i][j]=min(dp[i][j],dp[i][k]+dp[k + 1][j - 1]);
            }
        printf("Case %d: %d
",iCase,dp[1][n]);
    }
    return 0;
}


原文地址:https://www.cnblogs.com/zswbky/p/6792902.html