UVALive 4857 Halloween Costumes
区间dp。对于最左边的点:
1、在该点穿的衣服只有该点用的到,即穿上就脱下。所以dp[ l ][ r ] = min(dp[ l + 1][ r ] + 1, dp[ l ][ r ])。
2、衣服仍保留。这一步需要明确只有后面用到这件衣服才会选择保留。假如当前位置是i,j 位置和当前位置衣服相同。就可以考虑当前衣服一直穿到了 j 位置。因为在此期间该衣服可能还会被用到。所以我们把 j 位置保留下来,这样的话整个区间l ,r 就都可以用这件衣服了。 于是dp[ l ] [ r ] = min(dp[ l ][ r ], dp[l + 1][ j ] + dp[ j + 1][ r ] );
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define CLR(a, b) memset(a, b, sizeof(a)) using namespace std;
const int N = 110;
const int INF = 1e9 + 7; int dp[N][N];
int n, m, c[N]; int dfs(int l, int r)
{
if(l > r) return 0;
if(l == r) return 1;
if(dp[l][r] >= 0) return dp[l][r];
int &ret = dp[l][r];
ret = dfs(l + 1, r) + 1;
for(int i = l + 1; i <= r; i ++)
{
if(c[i] == c[l])
{
ret = min(ret, dfs(l + 1, i) + dfs(i + 1, r));
}
}
return ret;
} int main()
{
int t, cas = 1;
scanf("%d", &t);
while(t --)
{
scanf("%d%d", &n, &m);
c[0] = -1;
for(int i = 1; i <= n; i ++)
{
scanf("%d", &c[i]);
if(c[i] == c[i - 1])
n --, i --;
}
CLR(dp, -1);
printf("Case %d: %d\n", cas ++, dfs(1, n));
}
}
UVALive 4857 Halloween Costumes的更多相关文章
- UVA 4857 Halloween Costumes 区间背包
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- Lightoj 题目1422 - Halloween Costumes(区间DP)
1422 - Halloween Costumes PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 ...
- LightOj 1422 Halloween Costumes(区间DP)
B - Halloween Costumes Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit ...
- LightOJ - 1422 Halloween Costumes —— 区间DP
题目链接:https://vjudge.net/problem/LightOJ-1422 1422 - Halloween Costumes PDF (English) Statistics F ...
- LightOJ - 1422 Halloween Costumes (区间dp)
Description Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and he i ...
- Halloween Costumes(区间DP)
Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and he is planning t ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
- light oj 1422 Halloween Costumes (区间dp)
题目链接:http://vjudge.net/contest/141291#problem/D 题意:有n个地方,每个地方要穿一种衣服,衣服可以嵌套穿,一旦脱下的衣服不能再穿,除非穿同样的一件新的,问 ...
- 【LightOJ 1422】Halloween Costumes(区间DP)
题 题意 告诉我们每天要穿第几号衣服,规定可以套好多衣服,所以每天可以套上一件新的该号衣服,也可以脱掉一直到该号衣服在最外面.求最少需要几件衣服. 分析 DP,dp[i][j]表示第i天到第j天不脱第 ...
随机推荐
- Socket学习笔记(一)
1.socket介绍 我们知道两个进程如果需要进行通讯最基本的一个前提能能够唯一的标示一个进程,在本地进程通讯中我们可以使用PID来唯一标示一个进程,但PID只在本地唯一,网络中的两个进程PID冲突几 ...
- 【BZOJ 4148】 4148: [AMPPZ2014]Pillars (乱搞)
4148: [AMPPZ2014]Pillars Time Limit: 5 Sec Memory Limit: 256 MBSec Special JudgeSubmit: 100 Solve ...
- 【51Nod 1815】【算法马拉松 23】调查任务
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1815 tarjan缩点后在DAG上递推即可. 每个点维护所有根到它的路径 ...
- [BZOJ5361]/[HDU6291]对称数
[BZOJ5361]/[HDU6291]对称数 题目大意: 一个\(n(n\le2\times10^5)\)个结点的树,每个结点有一个权值\(a_i(a_i\le2\times10^5)\),\(m( ...
- 四、python之 if while for
一.if条件判断 if 条件判断: 逻辑操作…… …… else: 逻辑操作…… 其中"判断条件"成立时(非零),则执行后面的语句,而执行内容可以多行,以缩进来区分表示同一范围. ...
- web前端 -- onkeydown、onkeypress、onkeyup、onblur、onchange、oninput、onpropertychange的区别
FROM:http://www.cnblogs.com/svage/archive/2011/11/15/2249954.html onkeydown:按下任何键(字母.数字.系统.tab等)都能触发 ...
- 利用SharpZipLib对字符串进行压缩和解压缩
添加对ICSharpCode.SharpZipLib的引用. using ICSharpCode.SharpZipLib.BZip2; /// <summary> /// 压缩 /// & ...
- 2015 UESTC 搜索专题D题 基爷的中位数 二分
基爷的中位数 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Descr ...
- CSS的outline属性
input标签的outline的三个属性: outline-color outline-style outline-width 当设置input的focus状态的时候可以使用. input:focus ...
- ExtJS ComboBox同时加载远程和本地数据
ExtJS ComboBox同时加载远程和本地数据 原文:http://gblog.hbcf.net/index.php/archives/233 ComboBox比较特殊需求,将远程数据和本地数据同 ...