LightOJ - 1422 Halloween Costumes —— 区间DP
题目链接:https://vjudge.net/problem/LightOJ-1422
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。
1.dp[l][r]为在区间[l, r]内最少需要刷的次数。
2.在区间[l,r]内,我们对l进行讨论:
1)如果为左端点处刷上颜色时,仅仅是刷左端点处,而不延续到后面,那么状态可以转化为:dp[l][r] = 1+dp[l+1][r];)
2)如果为左端点处刷上颜色时,还延续到后面的区间。那么就枚举颜色刷到的右端点(前提是左右端点的颜色相同)。因为右端点和左端点的颜色是在同一次刷的,那么就可以把右端点处忽略,所以就转化为:dp[l][r] = dp[l][k-1] + dp[k+1][r]。枚举k,取所有情况的最小值即可。
记忆化搜索:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; int s[MAXN];
int dp[MAXN][MAXN]; int dfs(int l, int r)
{
if(l==r) return ;
if(l>r) return ;
if(dp[l][r]!=-) return dp[l][r]; dp[l][r] = +dfs(l+, r);
for(int k = l+; k<=r; k++)
if(s[l]==s[k])
dp[l][r] = min(dp[l][r], dfs(l, k-)+dfs(k+, r) ); return dp[l][r];
} int main()
{
int T, n;
scanf("%d", &T);
for(int kase = ; kase<=T; kase++)
{
scanf("%d", &n);
for(int i = ; i<=n; i++)
scanf("%d", &s[i]);
memset(dp, -, sizeof(dp));
int ans = dfs(, n);
printf("Case %d: %d\n", kase, ans);
}
}
递推:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; int s[MAXN];
int dp[MAXN][MAXN]; int main()
{
int T, n;
scanf("%d", &T);
for(int kase = ; kase<=T; kase++)
{
scanf("%d", &n);
for(int i = ; i<=n; i++)
scanf("%d", &s[i]); memset(dp, , sizeof(dp));
for(int i = ; i<=n; i++)
dp[i][i] = ;
for(int len = ; len<=n; len++)
{
for(int l = ; l<=n-len+; l++)
{
int r = l+len-;
dp[l][r] = + dp[l+][r];
for(int k = l+; k<=r; k++)
if(s[l]==s[k])
dp[l][r] = min(dp[l][r], dp[l][k-]+dp[k+][r]);
}
} printf("Case %d: %d\n", kase, dp[][n]);
}
}
写法二(虽然过了,但是想不明白为什么可行):
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; int s[MAXN];
int dp[MAXN][MAXN]; int dfs(int l, int r)
{
if(l==r) return ;
if(dp[l][r]!=-) return dp[l][r]; dp[l][r] = r-l+;
if(s[l]==s[r]) dp[l][r] = dfs(l, r-);
for(int k = l; k<r; k++)
dp[l][r] = min(dp[l][r], dfs(l, k)+dfs(k+, r) ); return dp[l][r];
} int main()
{
int T, n;
scanf("%d", &T);
for(int kase = ; kase<=T; kase++)
{
scanf("%d", &n);
for(int i = ; i<=n; i++)
scanf("%d", &s[i]);
memset(dp, -, sizeof(dp));
int ans = dfs(, n);
printf("Case %d: %d\n", kase, ans);
}
}
LightOJ - 1422 Halloween Costumes —— 区间DP的更多相关文章
- LightOJ 1422 Halloween Costumes 区间dp
题意:给你n天需要穿的衣服的样式,每次可以套着穿衣服,脱掉的衣服就不能再穿了,问至少要带多少条衣服才能参加所有宴会 思路:dp[i][j]代表i-j天最少要带的衣服 从后向前dp 区间从大到小 更新d ...
- light oj 1422 Halloween Costumes (区间dp)
题目链接:http://vjudge.net/contest/141291#problem/D 题意:有n个地方,每个地方要穿一种衣服,衣服可以嵌套穿,一旦脱下的衣服不能再穿,除非穿同样的一件新的,问 ...
- Light OJ 1422 - Halloween Costumes(区间DP 最少穿几件)
http://www.cnblogs.com/kuangbin/archive/2013/04/29/3051392.html http://www.cnblogs.com/ziyi--caolu/a ...
- LightOj 1422 Halloween Costumes(区间DP)
B - Halloween Costumes Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
- LightOJ - 1422 Halloween Costumes (区间dp)
Description Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and he i ...
- LightOJ 1422 Halloween Costumes 【 区间dp 】
区间dp的第一题----- 看题解看了好多~~终于看懂了---55555 dp[i][j] 表示第i天到第j天至少需要多少件衣服 那么第i件衣服只被第i天占用的话, dp[i][j] = dp[i+1 ...
- LightOJ 1422 Halloween Costumes (区间DP,经典)
题意: 有个人要去参加万圣节趴,但是每到一个趴都要换上特定的服装,给定一个序列表示此人要穿的衣服编号(有先后顺序的),他可以套很多件衣服在身上,但此人不喜欢再穿那些脱下的衣服(即脱下后就必须换新的), ...
- 【LightOJ 1422】Halloween Costumes(区间DP)
题 题意 告诉我们每天要穿第几号衣服,规定可以套好多衣服,所以每天可以套上一件新的该号衣服,也可以脱掉一直到该号衣服在最外面.求最少需要几件衣服. 分析 DP,dp[i][j]表示第i天到第j天不脱第 ...
随机推荐
- 关于Boot应用中集成Spring Security你必须了解的那些事
Spring Security Spring Security是Spring社区的一个顶级项目,也是Spring Boot官方推荐使用的Security框架.除了常规的Authentication和A ...
- Annual Congress of MUD
Annual Congress of MUD 时间限制: 1 Sec 内存限制: 128 MB 题目描述 Multiuser dungeon games, also called MUD games ...
- 【Tyvj1982】武器分配(费用流)
题意:有N个人要从A个物品中各取一个,B个物品中各取一个,选取第i个A类物品和第j个B类物品的费用是(a[i]-b[j])^2 求最小总花费 n<=a,b<=80 a[i],b[i]< ...
- 【BZOJ1305】dance跳舞(最大流,裂点,二分答案)
题意:一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲. 有一些男孩女孩相互喜欢,而其他相互不喜欢(不会“单向喜欢”). ...
- HDU 5001 Walk (暴力、概率dp)
Walk Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Sub ...
- Codeforces 864E Fire(DP)
题目链接 Fire 题意 有n个物品,每个物品的挽救时间代价为ti, 消失时刻为di, 价值为pi. 如果要救某个物品,必须在他消失之前救出来. 同一时刻最多只能救一件物品. 当前耗时为当前已经救出的 ...
- noip2013货车运输
P1967 货车运输 题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过 ...
- DTD概述
1. 什么是XML文件 可扩展标记语言,标准通用标记语言的子集,是用于标记电子文件使其具有结构性的标记语言. 2. 什么是dtd文件 DTD(文档类型定义)的作用是定义XML文档的合法构建模块.它使用 ...
- API调用开发demo
package fastjson; import java.io.BufferedReader;import java.io.IOException;import java.io.InputStrea ...
- iOS中创建自定义的圆角按钮
iOS中很多时候都需要用到指定风格的圆角按钮,尽管UIButton提供了一个方式创建圆角按钮: + (id)buttonWithType:(UIButtonType)buttonType;//指定bu ...