LightOJ - 1422 Halloween Costumes (section 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

The effect that a man dressed in n corresponds to day clothes, can matryoshka, that while wearing a few, of course, be off, but would not wear off, and asked at least need some clothes.

The feeling is not very easy to see that this is the interval DP ... borrow someone else's words:

Thinking: I could not think of a start interval dp, after which questions get, think of you every step either buy a new one, or take off, with the former, might be better off at present, but to buy a new, behind there are the clothes for several days, so obviously buy a new one is better, so that each do a choice will affect the back, and can not intuitively understand, is the general dp, and no significant linear state transition then I want to go out on the range 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

Since it is a given number of days it can be a series of interval division to take this number of days, dp [i] [j] represents the i j day to day requires a minimum of number of clothes. Then I do not know how to transfer the solution to a problem ... after reading other people came to understand, here to understanding the problem. Dp must first initialize the array, all dp [i] [i] = 1, then dp [i] [j] = dp [i-1] [j] +1, where well understood, but also the worst case that are worn every day, not a day off. Then analyze whether reducing the number of clothes ready. After a thorough wu nao dense Si ti kan cable jie, to discover one day (k) and if j day wearing the same clothes (as to why take out if the j-th day to between i j, because the analysis section DP range, while the range is constrained endpoints, so more convenient endpoint analysis), then later you can put on that dress in the k days, k + 1 ~ j-1 day Chuan Chuan Tuotuo, the final day of the first j has revealed such transfer equation also ready to come out. dp [i] [j] = min (dp [i] [j], dp [i] [k] + dp [k + 1] [j]).

Triplet code write cycle must be re-enumerated first interval length (small to large), representative of phase, the second point representative of the state of re-enumeration left (right end point is also be determined), k denotes the third decision re-enumeration , the order must not be reversed. note! ! In the second cycle in weight, have to make dp [i] [j] = dp [i] [j-1] +1, where it has been initialized can not not write this one, because there may be a dp after initialization [ i] [j] has been updated, resulting in less than the value of initialization, so i have to write it again here. (should be

#include <bits / STDC ++ H.>
 the using  namespace STD;
 int n-, A [ 105 ], DP [ 105 ] [ 105 ]; // DP [i] [j] represents the minimum number of pieces of the i-th day to the j-th day 
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 only to;
         for (as = 1 , only the <= n- 1 , just ++ )
         {
             for(i=1;i+len<=n;i++)
             {
                 dp [i] [i + len] = dp [i] [i len + 1 ] + 1 ; /// !! here as it has been initialized can not not write this one, because there may be an after initializing dp [i ] [j] has been updated, resulting in less than the value of initialization, so I have to write it again here. 
                 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;
}

 

Guess you like

Origin www.cnblogs.com/lipoicyclic/p/12514974.html