lightoj-1422-Halloween Costumes 区间dp

来源:互联网 发布:淘宝卖家电话在哪里 编辑:程序博客网 时间:2024/06/06 13:12

1422 - Halloween Costumes


PDF (English)StatisticsForum
Time Limit: 2 second(s)Memory Limit: 32 MB

Gappu has a very busy weekend ahead of him. Because, nextweekend 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 alwaysselects 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 willgo 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 theHalloween night, and wear costumes accordingly, he will be changing hiscostumes a number of times. So, to make things a little easier, he may put oncostumes 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 thecostumes, or wear a new one. That is, if he is wearing the Postman uniform overthe Superman costume, and wants to go to a party in Superman costume, he cantake off the Postman uniform, or he can wear a new Superman uniform. But, keepin 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 theHalloween night, if he needs the Postman costume again, he will have to use anew one. He can take off any number of costumes, and if he takes offkof the costumes, that will be the last k ones (e.g. if he wears costumeAbefore costumeB, to take off A, first he has to removeB).

Given the parties and the costumes, find the minimum numberof 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 integerN (1≤ N ≤ 100) denoting the number of parties. Next line containsNintegers, where the ith integer ci (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 numberof 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






对于区间[x, y), 如果有i使得a[x] == a[i], 则dp[x][y]=min(dp[x][i]+dp[i][y]-1, dp[x][y]), 否则dp[x][y]=dp[x][i]+dp[i][y]。


#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <cmath>#include <cstdlib>#include <vector>#include <queue>#include <stack>#include <set>#include <map>using namespace std;#define ll long long#define rep(x, y, z) for(int x=y;x<z;x++)const int maxn=1e2+7;int T;int n;int a[maxn];int dp[maxn][maxn];int kase;void init(){    memset(dp, 0, sizeof(dp));}void dfs(int x, int y){    if(y-x == 1) dp[x][y]=1;    rep(i, x+1, y){        if(dp[x][i] == 0) dfs(x, i);        if(dp[i][y] == 0) dfs(i, y);        if(a[i] == a[x]){            if(dp[x][y] != 0)  dp[x][y]=min(dp[x][y], dp[x][i]+dp[i][y]-1);            else dp[x][y]=dp[x][i]+dp[i][y]-1;        }        if(dp[x][y] != 0) dp[x][y]=min(dp[x][y], dp[x][i]+dp[i][y]);        else dp[x][y]=dp[x][i]+dp[i][y];    }}void solve(){    /*    rep(i, 0, n){        if(dp[0][i] == 0) dfs(0, i);        if(dp[i][n] == 0) dfs(i, n);        if(dp[0][n] != 0) dp[0][n]=min(dp[0][n], dp[0][i]+dp[i][n]);        else dp[0][n]=dp[0][i]+dp[i][n];    }     */    dfs(0, n);    printf("Case %d: %d\n", ++kase, dp[0][n]);}int main(){    //freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    scanf("%d", &T);    while(T--){        init();        scanf("%d", &n);        rep(i, 0, n){            scanf("%d", &a[i]);        }        solve();    }    return 0;}


0 0