【区间DP】——lightoj1422
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
|
题意:按顺序去参加舞会。每个舞会对衣服都有要求。可以连续穿好多件衣服。需要时候就脱下来,但是一旦脱下来,这件衣服就报废了。问最少需要几件衣服。
思路:
我们从后往前推导,(因为当前分析区间状态受到的是区间前面元素的影响)
dp[i][j]代表从区间i到区间j最少的穿衣数量,
那么在dp[i][j]这个状态的穿衣数,就要等于dp[i+1][j]+1;
也就是说,首先在不考虑它后面是否有一天要穿相同的衣服的情况下,它肯定会比区间i+1到j的衣服多出一件;
然后,再考虑在这个区间范围,是否有一天要穿相同的衣服,i<k<=j,如果有第k天衣服和第i天的衣服是一样的,那么就要比较如果第i天不穿1件衣服与第i天穿上1件衣服;
首先,第i天穿上一件衣服的结果已经得出,
那么我们只需比较不穿衣服,那么就是dp[i][j]=min(dp[i][j],dp[i+1][k-1]+dp[k][j]);
代码如下:
#include<iostream> #include<stdio.h> #include<string.h> using namespace std; int c[105]; int dp[105][105]; int main() { int t; cin>>t; for(int z=1;z<=t;z++) { int n; cin>>n; for(int i=1;i<=n;i++) cin>>c[i]; //初始化,假设这几天都穿不同衣服 memset(dp,0,sizeof(dp)); 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+1;j<=n;j++) { //假设每一件衣服都是有别于区间内部的衣服的 dp[i][j]=dp[i+1][j]+1; for(int k=i;k<=j;k++) { //如果相等则说明i+1到k-1区间内的衣服全部穿在第i个衣服只上 //所以可以把区间分成两部分 if(c[k]==c[i]) dp[i][j]=min(dp[i][j],dp[i+1][k-1]+dp[k][j]); } } } printf("Case %d: %d\n",z,dp[1][n]); } return 0; }

浙公网安备 33010602011771号