现在的位置: 首页 > 综合 > 正文

UVa12283 区间dp

2019年02月09日 ⁄ 综合 ⁄ 共 2885字 ⁄ 字号 评论关闭

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 contestbuddies, he would go with the costume
of `Chinese Postman'.

\epsfbox{p4857.eps}

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 

First line contains T (T$ \le$2500),
the number of test cases.

Each test case starts with two integers, N and M (1$ \le$NM$ \le$100),
the number of parties, and number of different types of costumes. Next line contains N integers, ci (1$ \le$ci$ \le$M),
the costume he will be wearing in party i. He will attend the party 1 first, then party 2, and so on.

There is a blank line before each test case.

Output 

For each test case, output the minimum number of required costumes. Look at the output for sample input for details.

Sample Input 

4 

1 1 
1 

2 2 
1 1 

3 2 
1 2 1 

7 3 
1 2 1 1 3 2 1

Sample Output 

Case 1: 1
Case 2: 1
Case 3: 2
Case 4: 4
这题在UVa中wa了三发,主要是oj不能识别PE,最后少写了一个换行。
这题只能用二维的dp来写,因为涉及到了区间的状态,所以应该用区间dp来写。
这里还用到了去重的函数n=unique(a+1,a+1+n)-a;
dp[i][j]表示[i,j]最少要穿多少的衣服。dp[i][j]=min(dp[i][k]+dp[k+1][j]-val) if(a[i]==a[k+1]) val=1,else val=0;
代码:
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#include<map>
#include<cstdio>
#include<cstring>
#define maxn 200
#define INF 0xfffffff
using namespace std;
int n,m;
int a[maxn],dp[maxn][maxn];
void init()
{
    for(int i=1;i<=n;i++)
    dp[i][i]=1;
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            dp[i][j]=INF;
        }
    }
}
int solve()
{
    int s,e;
    for(int len=1;len<n;len++)
    {
        for(int i=1;i+len<=n;i++)
        {
            s=i,e=i+len;
            for(int k=s;k<e;k++)
            {
                int tp=dp[s][k]+dp[k+1][e];
                if(a[s]==a[k+1])
                {
                    tp--;
                }
                dp[s][e]=min(dp[s][e],tp);
            }
        }
    }
    return dp[1][n];
}
int main()
{
    int t;
    cin>>t;
    for(int tt=1;tt<=t;tt++)
    {
        cin>>n>>m;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
        }
        n = unique(a+1, a + n+1) - a-1;//去掉连续的
        init();
        printf("Case %d: %d\n",tt,solve());
    }
    return 0;
}

抱歉!评论已关闭.