UVA12283 Halloween Costumes(区间dp)

    Gappu has a very busy weekend ahead of him. Because, next weekendis Halloween, and he is planning to attend as many parties ashe can. Since it’s Halloween, these parties are all costume parties,Gappu always selects his costumes in such a way that it blends withhis friends, that is, when he is attending the party, arranged by hiscomic-book-fan friends, he will go with the costume of Superman,but when the party is arranged contestbuddies, he would go withthe costume of ‘Chinese Postman’.

    Since he is going to attend a number of parties on the Halloweennight, and wear costumes accordingly, he will be changing his costumesa number of times. So, to make things a little easier, he mayput on costumes one over another (that is he may wear the uniformfor the postman, over the superman costume). Before each party hecan take off some of the costumes, or wear a new one. That is, if heis wearing the Postman uniform over the Superman costume, andwants to go to a party in Superman costume, he can take off thePostman uniform, or he can wear a new Superman uniform. But, keep in mind that, Gappu doesn’tlike to wear dresses without cleaning them first, so, after taking off the Postman uniform, he cannotuse that again in the Halloween 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 off k of the costumes, that will bethe 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 theHalloween night.


Input
First line contains T (T ≤ 2500), the number of test cases.
Each test case starts with two integers, N and M (1 ≤ N, M ≤ 100), the number of parties, and
number of different types of costumes. Next line contains N integers, ci (1 ≤ ci ≤ 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

题意:按顺序去参加舞会。每个舞会对衣服都有要求。可以连续穿好多件衣服。需要时候就脱下来,但是一旦脱下来,这件衣服就报废了。问最少需要几件衣服。

思路:区间dp,枚举区间长度和起点,如果a[i] == a[j],dp[i][j] = dp[i][j-1],否则dp[i][j] = dp[i][j-1]+1,然后枚举i到j之间的每个点,

看看能不能使dp[i][j]更小.

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<vector>
#include<cmath> 
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 1e5+5;
const double esp = 1e-7;
const int ff = 0x3f3f3f3f;
map<int,int>::iterator it;

int n,m;
int dp[110][110],c[110];

void init()
{
	mem(dp,0);
}

int main()
{
	int t,cnt = 0;
	cin>>t;
	while(t--)
	{
		init();
		
		scanf("%d %d",&n,&m);
		for(int i = 1;i<= n;i++)
			scanf("%d",&c[i]);
		
		for(int i = 1;i<= n;i++)
			dp[i][i] = 1;//初始状态
		for(int k = 1;k< n;k++)
		{
			for(int i = 1,j = i+k;j<= n;i++,j++)
			{
				if(c[i] == c[j])
					dp[i][j] = dp[i][j-1];
				else
					dp[i][j] = dp[i][j-1]+1;
				
				for(int x = i;x<= j;x++)
					dp[i][j] = min(dp[i][j],dp[i][x]+dp[x+1][j]);
			}
		}
		
		printf("Case %d: %d\n",++cnt,dp[1][n]);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值