LightOj 1422 Halloween Costumes 两种姿势详解(区间DP)

16 篇文章 3 订阅


1422 - Halloween Costumes
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 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

Output for Sample Input

2

4

1 2 1 2

7

1 2 1 1 3 2 1

Case 1: 3

Case 2: 4

 

题意: 题意有点难理解,有N个宴会,对于每一个宴会,女猪脚都要穿一种礼服,礼服可以套着穿,但是脱了的不能再用,参加宴会必须按顺序来,从第一个到第N个,问参加这些宴会最少需要几件礼服

思路:一开始可能想不到区间dp,这题拿到之后,会想,每一步要么买一件新的,要么脱掉,用以前的,可能目前脱掉比较好,但是买一件新的,后面有连续好多天都是这个衣服,这样显然是买一件新的比较好,这样每做一部选择都会影响后面的,而无法直观理解的,一般就是dp了,而且没有明显的线性状态转移那么就往区间dp上想了


姿势一:这个也就是大多数网上题解的姿势,从前往后枚举,可能影响后面的,而无法预知,这种通常就倒着想,逆着推,逆着枚举每一块区间,但每次枚举是从1到j-i那么枚举,不是像通常先把所有2的枚举完了,再枚举所有三的

dp[i][j]代表从区间i到区间j最少的穿衣数量,那么在dp[i][j]这个状态的穿衣数,就要等于dp[i+1][j]+1;也就是说,首先在不考虑它后面是否有一天要穿相同的衣服的情况下,它肯定会比区间i+1到j的衣服多出一件;

然后,再考虑在这个区间范围,是否有一天要穿相同的衣服,i<k<=j,如果有第k天衣服和第i天的衣服是一样的,那么就要比较如果第i天不穿1件衣服与第i天穿上1件衣服;

首先,第i天穿上一件衣服的结果已经得出,那么我们只需比较不穿衣服,那么就是dp[i][j]=min(dp[i][j],dp[i+1][k-1]+dp[k][j]);



代码:

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>

using namespace std;
int n;
int a[105];
int dp[105][105];
int main()
{
    int t;
    scanf("%d",&t);
    int cas=0;
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        memset(dp,0,sizeof(dp));

        for(int i=n;i>=1;i--)
        {
            for(int j=i;j<=n;j++) //从后往前枚举,并且后面的最优解已经枚举过了,因为实际是从前往后进行,所以从后枚举不会造成影响
            {
                dp[i][j]=dp[i+1][j]+1;  //先直接给他加上,因为说不定这件衣服后面好多件都要用到
                for(int k=i+1;k<=j;k++)
                {
                    if(a[i]==a[k])
                    dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k+1][j]);  //这里不要是i+1,不是i,因为在讨论是不是脱i呢
                }
            }
        }
        printf("Case %d: %d\n",++cas,dp[1][n]);

    }
    return 0;
}
姿势二:普通区间dp做法,枚举所有长度,再枚举起点,这样从前面枚举就好了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1e3;
int dp[maxn][maxn], col[maxn];
int main()
{
    int t, ca = 1, n;
    cin >> t;
    while(t--)
    {
        cin >> n;
        for(int i = 1;i <= n; i++)
            cin >> col[i];
        for(int i = 1; i <= n; i++)
            for(int j = i; j <= n; j++)
                dp[i][j] = j-i+1;
        for(int len = 1; len < n; len++) //枚举所有区间长度
        {
            for(int i = 1; i+len <= n; i++)  //枚举起点
            { 
                int j = i+len;
                dp[i][j] = dp[i][j-1]+1;
                for(int k = 1; k < j; k++)
                {
                    if(col[k] == col[j])
                        dp[i][j] = min(dp[i][j], dp[i][k]+dp[k+1][j-1]);
                }
            }
        }
        printf("Case %d: %d\n", ca++, dp[1][n]);
    }
    return 0;
}




评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值