题目链接:https://vjudge.net/problem/LightOJ-1422

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。

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的更多相关文章

  1. LightOJ 1422 Halloween Costumes 区间dp

    题意:给你n天需要穿的衣服的样式,每次可以套着穿衣服,脱掉的衣服就不能再穿了,问至少要带多少条衣服才能参加所有宴会 思路:dp[i][j]代表i-j天最少要带的衣服 从后向前dp 区间从大到小 更新d ...

  2. light oj 1422 Halloween Costumes (区间dp)

    题目链接:http://vjudge.net/contest/141291#problem/D 题意:有n个地方,每个地方要穿一种衣服,衣服可以嵌套穿,一旦脱下的衣服不能再穿,除非穿同样的一件新的,问 ...

  3. Light OJ 1422 - Halloween Costumes(区间DP 最少穿几件)

    http://www.cnblogs.com/kuangbin/archive/2013/04/29/3051392.html http://www.cnblogs.com/ziyi--caolu/a ...

  4. LightOj 1422 Halloween Costumes(区间DP)

    B - Halloween Costumes Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit ...

  5. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

  6. LightOJ - 1422 Halloween Costumes (区间dp)

    Description Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and he i ...

  7. LightOJ 1422 Halloween Costumes 【 区间dp 】

    区间dp的第一题----- 看题解看了好多~~终于看懂了---55555 dp[i][j] 表示第i天到第j天至少需要多少件衣服 那么第i件衣服只被第i天占用的话, dp[i][j] = dp[i+1 ...

  8. LightOJ 1422 Halloween Costumes (区间DP,经典)

    题意: 有个人要去参加万圣节趴,但是每到一个趴都要换上特定的服装,给定一个序列表示此人要穿的衣服编号(有先后顺序的),他可以套很多件衣服在身上,但此人不喜欢再穿那些脱下的衣服(即脱下后就必须换新的), ...

  9. 【LightOJ 1422】Halloween Costumes(区间DP)

    题 题意 告诉我们每天要穿第几号衣服,规定可以套好多衣服,所以每天可以套上一件新的该号衣服,也可以脱掉一直到该号衣服在最外面.求最少需要几件衣服. 分析 DP,dp[i][j]表示第i天到第j天不脱第 ...

随机推荐

  1. scrapy介绍及源码分析

    一 简介 Scrapy基于事件驱动网络框架 Twisted 编写.因此,Scrapy基于并发性考虑由非阻塞(即异步)的实现. 官方文档 :https://docs.scrapy.org/en/late ...

  2. 16.1116 NOIP 考前模拟(信心题)

    分火腿 (hdogs.pas/.c/.cpp) 时间限制:1s:内存限制 64MB 题目描述: 小月言要过四岁生日了,她的妈妈为她准备了n根火腿,她想将这些火腿均分给m位小朋友,所以她可能需要切火腿. ...

  3. msp430项目编程41

    msp430综合项目---红外遥控直流电机调速系统41

  4. plsql + 客户端 连接oracle数据库

    一. 目录结构D:\oracle\instantclient_11_2D:\oracle\instantclient_11_2\tnsnames.ora 二. 环境变量 NLS_LANG = SIMP ...

  5. T2597 团伙 codevs

    http://codevs.cn/problem/2597/  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 1920年的芝加 ...

  6. rm -rf /* 注意

    mkdir -p ~/.trash  //创建一个目录作为回收站,这里使用的是用户家目录下的.trash目录 alias rm=trash  //命令别名 rm改变为trash,通过将rm命令别名值t ...

  7. Solidworks如何创建投影曲线

    画好草图之后(草图是在上视基准面上画的)然后点击曲线,投影曲线   面选择要投影的曲面,然后就得到了平面曲线在曲面上的投影得到的空间曲线   注意这种方法对于开环轮廓也是可以用的,比如下面,我定义一个 ...

  8. 如何正确地在React中处理事件

    1.构造器内绑定this class MyComponent extends React.Component { constructor(props) { super(props); this.sta ...

  9. mui + vue 模板

    示例代码: (function(mui, doc) { // 定义全局变量(计时器) var timer; // mui初始化 mui.init(); // 创建vue的实例 var app = ne ...

  10. 关于Java中强制类型转换的问题

    为了更好的理解我们先看下面的例子: package com.yonyou.test; import java.util.ArrayList; import java.util.Iterator; im ...