0
点赞
收藏
分享

微信扫一扫

LightOj 1422 Halloween Costumes(区间dp)

码农K 2022-06-17 阅读 82

区间dp
区间dp就是在区间上进行动态规划,求解一段区间上的最优解。主要是通过合并小区间的最优解进而得出整个大区间上最优解的dp算法。
把这个区间分割成一个个小区间,求解每个小区间的最优解,再合并小区间得到大区间即可。
可以枚举区间长度len为每次分割成的小区间长度(由短到长不断合并),内层枚举该长度下可以的起点,自然终点也就明了了。然后在这个起点终点之间枚举分割点,求解这段小区间在某个分割点下的最优解。

模板

memset(dp, 0, sizeof(dp));
//for(int i=1; i<=n; i++)
//dp[i][i]=1;
for(int len=1; len<=n; len++){ //区间长度
for(int i=1; i<=n; i++){ //枚举起点
int j=i+len-1; //区间终点
if(j>n)
break; //越界结束
dp[i][j]=dp[i][j-1]+1; //看情况加
for(int k=i; k<j; k++){ //枚举分割点,构造状态转移方程
if(a[k]==a[j])
dp[i][j]=min(dp[i][j], dp[i][k]+dp[k+1][j-1]);
}
}
}

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

注意是所有的派对都要参加,求购买衣服的最少数量
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]);

PS:现在还没看懂这道题

普通区间dp

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int dp[1000][1000],a[1000];
int t, n, cas=0;
int main()
{
scanf("%d", &t);
while(t--){
scanf("%d", &n);
for(int i=1; i<=n; i++)
scanf("%d", &a[i]);
memset(dp, 0, sizeof(dp));
// for(int i=1; i<=n; i++)//初始化每个派对都要穿一件衣服
// dp[i][i]=1;//其实也可以不加,因为后面 dp[i][j]=dp[i][j-1]+1;会自己加上
for(int len=1; len<=n; len++){ //区间长度
for(int i=1; i<=n; i++){ //枚举起点
int j=i+len-1; //区间终点
if(j>n)
break; //越界结束
dp[i][j]=dp[i][j-1]+1;
//不管用不用穿,先穿上:从i到j的区间穿上的衣服等于从i到j-1穿上的衣服再加1
//如果不用穿的话,再后面的min比较中会优化
for(int k=i; k<j; k++){ //枚举分割点,构造状态转移方程
if(a[k]==a[j]) //k有同一件
dp[i][j]=min(dp[i][j], dp[i][k]+dp[k+1][j-1]); //a[k]==a[j],第j个和第k个一样,不用再穿
}
}
}
printf("Case %d: %d\n", ++cas, dp[1][n]);//dp[1][n]为从1到n个派对穿上最少的衣服
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int a[105], dp[105][105];
int n, cas=0, t;
int main()
{
scanf("%d", &t);
while (t--){
scanf("%d", &n);
for (int i=1; i<=n; i++)
scanf("%d", &a[i]);
memset(dp, 0, sizeof(dp));
for (int i=n; 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]+dp[k+1][j]);
}
}
}
printf("Case %d: %d\n", ++cas, dp[1][n]);
}
return 0;
}


举报

相关推荐

0 条评论