lightoj-1422-Halloween Costumes 区间dp

本文介绍了一个关于万圣节服装选择的算法问题,主人公Gappu为了参加多个主题各异的派对,需要高效地更换服装。文章详细阐述了如何通过动态规划的方法找到所需最少的服装数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1422 - Halloween Costumes


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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值