(LightOJ

来源:互联网 发布:mysql.sock 编辑:程序博客网 时间:2024/05/21 07:52

(LightOJ - 1422)Halloween Costumes

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个party,第i场party需要穿a[i]风格的衣服,他为了方便可以衣服外面套衣服,在参加party时可以脱掉外面的衣服露出里面的衣服而适应当场party的风格,脱掉的衣服不能再用,问最少需要穿多少次衣服。

思路:题目要求类似于栈,要露出以前的衣服必须把目标衣服之前的衣服全部脱掉。对于每一件要去参加party的衣服都有新穿一件和不穿而选择脱两种状态,设f[i][j]表示第i场party到第j场party的最少穿衣次数,a[i]表示i场party对应的衣服。考虑状态转移:①j场时新穿一件,f[i][j]=f[i][j1]+1;②j场时不穿选择脱,这时前面必须有一场party的衣服和j这场相同,即有a[k]==a[j](i<=k< j),f[i][j]=minf[i][k]+f[k+1][j1],这样的递推式保证了j场party时能穿到符合条件的衣服。再在两种之中取最小值就是所要求的答案。

递推写法:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int INF=0x3f3f3f3f;const int maxn=105;int a[maxn],f[maxn][maxn];int main(){    int T,kase=1;    scanf("%d",&T);    while(T--)    {        memset(f,0,sizeof(f));        int n;        scanf("%d",&n);        for(int i=1;i<=n;i++)        {            scanf("%d",a+i);            f[i][i]=1;        }         for(int l=2;l<=n;l++)            for(int i=1;i+l-1<=n;i++)            {                int L=i,R=i+l-1;                f[L][R]=INF;                f[L][R]=f[L][R-1]+1;                for(int k=L;k<R;k++)                    if(a[k]==a[R])                        f[L][R]=min(f[L][R],f[L][k]+f[k+1][R-1]);            }        printf("Case %d: %d\n",kase++,f[1][n]);    }    return 0;}

记忆化搜索写法:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int INF=0x3f3f3f3f;const int maxn=105;int a[maxn],f[maxn][maxn];int dfs(int i,int j){    if(i>j) return 0;    if(i==j) return 1;    if(f[i][j]!=-1) return f[i][j];    f[i][j]=dfs(i,j-1)+1;    for(int k=i;k<j;k++)    {        if(a[j]==a[k])             f[i][j]=min(f[i][j],dfs(i,k)+dfs(k+1,j-1));    }    return f[i][j];}int main(){    int T,kase=1;    scanf("%d",&T);    while(T--)    {        int n;        scanf("%d",&n);        for(int i=1;i<=n;i++)        {            scanf("%d",a+i);        }        memset(f,-1,sizeof(f));        printf("Case %d: %d\n",kase++,dfs(1,n));    }    return 0;}
原创粉丝点击