LightOJ - 1422 Halloween Costumes(区间DP)

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

大意就是一个人要在n天穿着对应的衣服,可以套娃,即同时穿几件,当然也可以脱下来,但是脱下来的就不能再穿了,问最少需要准备几件衣服。

感觉并不是很容易就看出来这是区间DP...借用别人的一段话:

思路:一开始可能想不到区间dp,这题拿到之后,会想,每一步要么买一件新的,要么脱掉,用以前的,可能目前脱掉比较好,但是买一件新的,后面有连续好多天都是这个衣服,这样显然是买一件新的比较好,这样每做一部选择都会影响后面的,而无法直观理解的,一般就是dp了,而且没有明显的线性状态转移那么就往区间dp上想了

https://blog.csdn.net/qq_34374664/article/details/54867113?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

既然给的是一系列天数那么可以拿这段天数进行区间划分,dp[i][j]代表第i天到第j天最少需要的衣服件数。然后我就不知道怎么转移了...后来看了别人的题解才明白, 这里要理解题意。首先要对dp数组进行初始化,所有的dp[i][i]=1,然后dp[i][j]=dp[i-1][j]+1,这里很好理解,最差情况也就是每天都在穿,没有一天脱。然后分析能否减少准备的衣服件数。经过缜wu密nao的kan思ti索jie,能够发现如果i到j之间的某一天(k)如果和第j天穿的衣服 一样(至于为什么拿第j天出来,因为区间DP分析的是区间,而区间被端点约束,所以分析端点较方便),那么可以在第k天穿上那件衣服以后,在k+1~j-1天穿穿脱脱,最终在第j天露了出来,这样转移方程也就呼之欲出了。dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j])。

代码里写三重循环,第一重一定要枚举区间长度(从小到大),代表阶段,第二重枚举左端点代表状态(同时也可以确定右端点),第三重枚举k代表决策,顺序一定不能颠倒。注意!!在第二重循环里,先要令dp[i][j]=dp[i][j-1]+1,这里不能因为已经初始化了就不写这一句,因为初始化之后可能某个dp[i][j]又被更新了,导致其比初始化的值小,所以这里还得写一遍。(应该是这样

#include <bits/stdc++.h>
using namespace std;
int n,a[105],dp[105][105];//dp[i][j]表示第i天到第j天的最少件数 
int main()
{
    int t;
    cin>>t;
    int cnt=0;
    while(t--)
    {
        cnt++;
        cin>>n;
        memset(dp,0,sizeof(dp));
        int i,j;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        for(i=1;i<=n;i++)
        {
            for(j=i;j<=n;j++)
            {
                if(j==i)dp[i][j]=1;
                else dp[i][j]=dp[i][j-1]+1;
            }
         }
         int len,k;
         for(len=1;len<=n-1;len++)
         {
             for(i=1;i+len<=n;i++)
             {
                 dp[i][i+len]=dp[i][i+len-1]+1;///!!这里不能因为已经初始化了就不写这一句,因为初始化之后可能某个dp[i][j]又被更新了,导致其比初始化的值小,所以这里还得写一遍。 
                 for(j=i;j<=i+len;j++)
                 {
                     if(a[j]==a[i+len])
                     {
                         dp[i][i+len]=min(dp[i][i+len],dp[i][j]+dp[j+1][i+len-1]);
                     }
                }
            }
         }
         printf("Case %d: %d\n",cnt,dp[1][n]);
    }
    
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/lipoicyclic/p/12514974.html