Light oj 1422 Halloween Costumes 区间DP ★★★

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

 


PROBLEM SETTER: MANZURUR RAHMAN KHAN
SPECIAL THANKS: JANE ALAM JAN (SOLUTION, DATASET)

对于此题,dp[le][ri]的意义就是对于区间[le,ri]的子问题所需的最小答案(最少衣服数量)。
顺序的大致方向肯定是从左往右,假如之前考虑了第x天以前的情况,现在我要来考虑刚刚包含第 x天的情形了:
那么有两种方案:
1.我可以新增一件衣服来满足第x天的衣服要求
 dp[le][ri]=dp[le][ri-1]+1;

2.通过脱衣服来满足

假设存在i∈[le,ri-1],有a[i]==a[ri](a[i]表示第i天要求的服装样式)
那么dp[le][ri]= min{dp[le][i]+dp[i+1][ri-1] };


最后答案是考虑1、2两种情况取最小
 dp[le][ri]=dp[le][ri-1]+1;
  dp[le][ri]=min(dp[le][ri-1]+1,min{DP[le][i]+DP[i+1][ri-1]} );

意思就是dp[le][i]这个区间最后一件衣服是a[i],和a[ri]一样。
要通过脱衣服,使得第ri天满足需要,

那么我就要保证区间[i+1,ri-1]这个区间不能把a[i]给脱了,

那么考虑[i+1,ri-1]这个区间时,必须要像前面没有穿任何衣服一样考虑。
而这种考虑方式,这种意义,不就是和dp[1,n](所求)一样么?
划分子问题的精髓所在

这个题区间中的关键就在于区间的一头一尾,对于区间[le,ri],考虑完这个区间,最外面的衣服一定是和a[ri]值相等的,而且le之前是没有穿过任何衣服的。

我想说这题之前想了很半天,刚开始一直认为很难,今天中午队友告诉我他以前做过,这题不难,我又想,结果趴在床上迷迷糊糊想出来了。

区间DP的思想主要是分治。所以第一步就是划分子问题,这点尤其重要,

状态的意义也就是由此而来。

对于一个区间DP问题,我们关键要做的就是找到一个连续的,易于表示的区间

为什么呢?因为如果找到不连续的,零散无关联的多个元素,状态的表示非常之难。

因为会占用太多空间。如果是一个连续的区间,那么就非常好表示,比如数组中连续的一段,我就可以表示为[le,ri];

实际的过程是纷繁复杂的,动态规划就是要通过合理地设计顺序,合理地表示状态,来达到简化计算量的目的。


#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<fstream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<cctype>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI (4.0*atan(1.0))
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)
#define  lson   ind<<1,le,mid
#define rson    ind<<1|1,mid+1,ri
#define MID   int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mk    make_pair
#define _f     first
#define _s     second
#define ysk(x)  (1<<(x))
using namespace std;
typedef long long ll;
//const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
const int INF =0x3f3f3f3f;
const int maxn=100+10    ;
//const int maxV=12    ;
int a[maxn];
int n;

int dp[maxn][maxn];
int DP(int le,int ri)
{
    if(~dp[le][ri])    return dp[le][ri];
    if(le>ri)  return dp[le][ri]=0;
    if(le==ri)  return dp[le][ri]=1;
    dp[le][ri]=DP(le,ri-1)+1;
    for(int i=le;i<=ri-1;i++) if(a[i]==a[ri])
    {
        dp[le][ri]=min(dp[le][ri],DP(le,i)+DP(i+1,ri-1) );
    }
    return dp[le][ri];


}
int main()
{
    int T,kase=0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        memset(dp,-1,sizeof dp);
        printf("Case %d: %d\n",++kase,DP(1,n));
    }

   return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值