Light OJ 1422-Halloween costumes (interval DP)

Source: Internet
Author: User
1422-Halloween costumes
PDF (English) Statistics Forum
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 partition 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 wocould 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, canhe not 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 offKOf the costumes, that will be the lastKOnes (e.g. If he wears costumeABefore costumeB, To take offA, First he has to removeB).

Given the parties and the costumes, find the minimum number of costumes Gappu will need in the Halloween night.

Input

Input starts with an integerT (≤ 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, whereIthIntegerCi (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 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

 


Question:

The style of the clothes you need to wear in N days. Each time you wear a dress, the clothes you take off will no longer be used (you can wear it again, but it is cost ), ask how many clothes should be taken to all banquets.


Ideas:

It is obviously the interval DP, and DP [I] [J] indicates I ~ The minimum number of days required.

Consider J-day wear, if so DP [I] [J] = DP [I] [J-1] + 1;

If you do not wear a K (I <= k <j), the J-day is the same as the K-day clothes, and the k + 1 ~ J-1 all take off after wearing, so

DP [I] [J] = DP [I] [k] + dp [k + 1] [J-1];

This problem was solved happily, with the interval DP!


Code:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#define maxn 105#define MAXN 100005#define mod 100000000#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6typedef long long ll;using namespace std;int n,m,ans,cnt,tot;int a[maxn],dp[maxn][maxn];int main(){    int i,j,t,test=0;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        for(i=1;i<=n;i++)        {            scanf("%d",&a[i]);        }        memset(dp,0,sizeof(dp));        for(i=1;i<=n;i++) dp[i][i]=1;        for(j=2;j<=n;j++)        {            for(i=1;i<j;i++)            {                dp[i][j]=dp[i][j-1]+1;                for(int k=i;k<j;k++)                {                    if(a[k]==a[j]) dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j-1]);                }            }        }        ans=dp[1][n];        printf("Case %d: %d\n",++test,ans);    }    return 0;}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.