本文主要是介绍LightOJ 1422 Halloween Costumes,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
原题:
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 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.
For each case, print the case number and the minimum number of required costumes.
样例输入
2
4
1 2 1 2
7
1 2 1 1 3 2 1
输出
Case 1: 3
Case 2: 4
中文:
你要参加一串舞会,参加不同的舞会需要穿不同的礼服,你穿礼服可以像套娃一样,外面套好多层,不过脱下来的衣服就不能再穿上了,问你最少穿多少衣服可以参加所以的舞会。
代码:
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
typedef long long ll;typedef pair<int,int> pii;
const int maxn = 105;
int a[maxn];
int dp[maxn][maxn];
int n;int dfs(int i,int j)
{if(j<i)return 0;if(i==j){dp[i][j]=1;return 1;}if(dp[i][j]>0)return dp[i][j];dp[i][j]=dfs(i,j-1)+1;for(int k=i;k<j;k++){if(a[k]==a[j])dp[i][j]=min(dp[i][j],dfs(i,k)+dfs(k+1,j-1));}return dp[i][j];
}
int t;
int main()
{ios::sync_with_stdio(false);cin>>t;int k =0;while(t--){memset(dp,0,sizeof(dp));cin>>n;for(int i=1;i<=n;i++)cin>>a[i];int ans = dfs(1,n);cout<<"Case "<<++k<<": "<<ans<<endl;}return 0;
}
思路:
kuangbing大神的区间dp练习场,说啥也没看出这题怎么用区间的关系来描述状态。不得不说匡神的题目质量还真是高。
既然都说是区间dp了,肯定是用两个下标来描述一段区间的状态
设置 d p [ i ] [ j ] dp[i][j] dp[i][j]表示参加第i场舞会到第j场舞会所需要带的最少衣服
此题容易给人一种错觉,由于参加舞会是第一个到最后一个依次参加,很容易让人想到DAG模型,如果使用区间模型,那么如何保证当前描述的区间状态的最优解,是不受到序列顺序的影响呢?
一般的区间dp,考虑决策方向都是可以从两个方向考虑的,即下标i和下标j,这也是一个思维误区,认为区间动态规划的状态转移,需要能给够在两个方向上进行。其实不是这样, 区间dp也同样只可以在同一个方向上进行状态转移,以上一个区间作为候选状态,向前递推。
那么本题目的状态转移方程可以表示为
如果参加第j个舞会,选择直接穿上一件第j个舞会要求的礼服
d p [ i ] [ j ] = m i n ( d p [ i ] [ j ] , d p [ i ] [ j − 1 ] + 1 ) dp[i][j]=min(dp[i][j],dp[i][j-1]+1) dp[i][j]=min(dp[i][j],dp[i][j−1]+1)
如果采用“套娃”式穿衣策略,此时枚举区间[i,j]中你参加过的第k个舞会,要求穿的礼服与第j个舞会的礼服相同,你可以在参加第j个舞会时,把参加[k+1,j-1]的舞会时的衣服脱掉
注意,此时的状态不应该是 d p [ i ] [ j ] = m i n ( d p [ i ] [ j ] , d p [ i ] [ k ] ) dp[i][j]=min(dp[i][j],dp[i][k]) dp[i][j]=min(dp[i][j],dp[i][k])
而是
d p [ i ] [ j ] = m i n ( d p [ i ] [ j ] , d p [ i ] [ k ] + d p [ k + 1 ] [ j − 1 ] ) dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j-1]) dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j−1])
因为你参加[k+1,j-1]个舞会的时候也要穿礼服,这也是为什么要用区间模型的原因
这篇关于LightOJ 1422 Halloween Costumes的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!