首页 > 其他分享 >LightOJ 1422 Halloween Costumes (区间DP)

LightOJ 1422 Halloween Costumes (区间DP)

时间:2023-06-12 14:33:10浏览次数:41  
标签:1422 LightOJ Halloween costumes number costume include dp he


题意:你要连续去很多个舞会,给出n个舞会你需要穿的衣服的编号,一旦脱下就不能再穿,但是可以一件套一件,问最少需要准备多少件衣服。

思路:区间DP,令dp[i][j]为第i到第j天需要的衣服,那么对于第i天,如果考虑后面没有和它重复的话,那么dp[i][j]=dp[i+1][j]+1, 如果存在某一天a[i]==a[k],dp[i][j]=min(dp[i][j],dp[i+1][k-1]+dp[k][j])



#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 105
#define LL long long
int cas=1,T;
int dp[maxn][maxn];
int a[maxn];
int main()
{
	//freopen("in","r",stdin);
	scanf("%d",&T);
	while (T--)
	{
		int n;
		scanf("%d",&n);
		for (int i = 1;i<=n;i++)
			scanf("%d",&a[i]);
		for (int i = 1;i<=n;i++)
			for (int j = i;j<=n;j++)
				dp[i][j]=j-i+1;
		for (int i = n-1;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-1]+dp[k][j]);
				}
			}
		}
		printf("Case %d: %d\n",cas++,dp[1][n]);
	}
	//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}




Description



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


2

4

1 2 1 2

7

1 2 1 1 3 2 1


Sample Output


Case 1: 3

Case 2: 4




标签:1422,LightOJ,Halloween,costumes,number,costume,include,dp,he
From: https://blog.51cto.com/u_16156555/6462556

相关文章

  • LightOJ - 1042 Secret Origins (模拟)水
    TimeLimit: 500MSMemoryLimit: 32768KB64bitIOFormat: %lld&%lluLightOJ-1042SecretOriginsSubmit StatusDescriptionThisisthetaleofZephyr,thegreatesttimetravelertheworldwillneverknow.EventhosewhoareawareofZephyr'sexiste......
  • LightOJ - 1048 Conquering Keokradong (二分)输出路径
    TimeLimit: 1000MSMemoryLimit: 32768KB64bitIOFormat: %lld&%lluLightOJ-1048ConqueringKeokradongSubmit StatusDescriptionThiswinterwearegoingonatriptoBandorban.ThemaintargetistoclimbuptothetopofKeokradong.So,wewilluse......
  • LightOJ - 1076 Get the Containers (二分)模板题
    TimeLimit: 2000MSMemoryLimit: 32768KB64bitIOFormat: %lld&%lluLightOJ-1076GettheContainersSubmit StatusDescriptionAconveyorbelthasanumberofvesselsofdifferentcapacitieseachfilledtobrimwithmilk.Themilkfromconveyorbeltis......
  • LightOJ - 1374 Confusion in the Problemset (模拟)
    TimeLimit: 2000MSMemoryLimit: 32768KB64bitIOFormat: %lld&%lluLightOJ-1374ConfusionintheProblemsetSubmit StatusDescriptionAsmallconfusioninaproblemsetmayruinthewholecontest.So,mostoftheproblemsetterstrytheirbesttorem......
  • leetcode-1422-easy
    MaximumScoreAfterSplittingaStringGivenastringsofzerosandones,returnthemaximumscoreaftersplittingthestringintotwonon-emptysubstrings(i.e.leftsubstringandrightsubstring).Thescoreaftersplittingastringisthenumberofze......
  • LightOJ - 1058 Parallelogram Counting (数学几何&技巧)给n个点求组成平行四边形个数
    LightOJ-1058ParallelogramCountingTimeLimit: 2000MSMemoryLimit: 32768KB64bitIOFormat: %lld&%lluSubmit StatusDescriptionThereare n distinctpointsintheplane,givenbytheirintegercoordinates.Findthenumberofparallelogramswhosever......
  • LightOJ1007---Mathematically Hard (欧拉函数)
    Mathematicallysomeproblemslookhard.Butwiththehelpofthecomputer,someproblemscanbeeasilysolvable.Inthisproblem,youwillbegiventwointegersaandb.Youhavetofindthesummationofthescoresofthenumbersfromatob(inclusive).T......
  • LightOJ 1348 Aladdin and the Return Journey (树链剖分)
    树链剖分模板题。最近一直有比赛。。好长时间没写了。明显生疏了。。找个模板题熟悉一下。代码如下:#include<iostream>#include<string.h>#include<math.h>#include<queue>#include<algorithm>#include<stdlib.h>#include<map>#include<set>#include......
  • LightOJ - 1044 Palindrome Partitioning(DP)
    题目大意:给你一个字符串,要求你对字符串进行划分,使得划分出来的子串都是回文串,且子串数量达到最小解题思路:用dp[i]表示前i个字符划分成回文串,需要划分成多少个部分接着枚举j,如果[i,j]回文,那么dp[i]=min(dp[i],dp[j-1]+1)#include<cstdio>#include<cstring>#include<al......
  • LightOJ - 1300 Odd Personality(边双连通+奇圈判定)
    题目大意:给出一张无向图,要求找出符合条件的点条件如下:从该点出发,经过一定数量的边,又回到该点,经过的边不能重复经过,且经过的边的数量为奇数解题思路:要回到原点,且不能重复经过边,只能在边双连通分量中找了接着要判断的是有多少个点,只要边双连通分量中有奇圈,那么这个连通分量中的所......