状态压缩---区间dp第一题
标签: ACM
题目
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题,看得我头有点晕
题意为给你几个需要穿的衣服编号你需要依次穿上,可以套着穿,但是拖了不能再穿同一件(要增加次数),求最少的衣服数量
从最后一天往第一天dp,可以选择直接套上则dp[i][j]=dp[i+1][j]+1,如果中间有相同一套的衣服,可以选择把衣服脱了,则dp[i][j]=dp[i+1][k]+dp[k+1][j],再对这两个值取最小值即为dp[i][j]最小值
AC代码
#include <iostream>
#include <string.h>
using namespace std;
int dp[105][105];
int dress[105];
int main()
{
int t,n,i,j,k,x;
while(cin>>t)
for(i=1;i<=t;i++)
{
memset(dp,0,sizeof(dp));
cin>>n;
for(j=0;j<n;j++)
cin>>dress[j];
for(j=0;j<n;j++)
dp[j][j]=1;
for(j=n-1;j>=0;j--)
for(k=j+1;k<n;k++)
{
dp[j][k]=dp[j+1][k]+1;
for(x=j+1;x<=k;x++)
if(dress[x]==dress[j])
dp[j][k]=min(dp[j][k],dp[j+1][x]+dp[x+1][k]);
}
cout<<"Case "<<i<<": "<<dp[0][n-1]<<endl;
}
return 0;
}
状态压缩---区间dp第一题的更多相关文章
- ACM学习历程—HDU1584 蜘蛛牌(动态规划 && 状态压缩 || 区间DP)
Description 蜘蛛牌是windows xp操作系统自带的一款纸牌游戏,游戏规则是这样的:只能将牌拖到比她大一的牌上面(A最小,K最大),如果拖动的牌上有按顺序排好的牌时,那么这些牌也跟着一起 ...
- poj 2955 Brackets (区间dp基础题)
We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a ...
- 又一道区间DP的题 -- P3146 [USACO16OPEN]248
https://www.luogu.org/problemnew/show/P3146 一道区间dp的题,以区间长度为阶段; 但由于要处理相邻的问题,就变得有点麻烦; 最开始想了一个我知道有漏洞的方程 ...
- 状态压缩dp第一题
标签: ACM 题目: Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; ...
- 状态压缩---状态压缩dp第一题
标签: ACM 题目: Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; ...
- POJ 2441 Arrange the Bulls 状态压缩递推简单题 (状态压缩DP)
推荐网址,下面是别人的解题报告: http://www.cnblogs.com/chasetheexcellence/archive/2012/04/16/poj2441.html 里面有状态压缩论文 ...
- B1068 [SCOI2007]压缩 区间dp
这个题我状态想对了,但是转移错了...dp的代码难度都不大,但是思考含量太高了..不会啊,我太菜了. 其实这个题就是一个正常的区间dp,中间多了一个特判的转移就行了. 题干: Description ...
- 【BZOJ-1068】压缩 区间DP
1068: [SCOI2007]压缩 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1001 Solved: 615[Submit][Status][ ...
- hdu 4649 Professor Tian 反状态压缩+概率DP
思路:反状态压缩——把数据转换成20位的01来进行运算 因为只有20位,而且&,|,^都不会进位,那么一位一位地看,每一位不是0就是1,这样求出每一位是1的概率,再乘以该位的十进制数,累加,就 ...
随机推荐
- Valera and Swaps
题意: 定义 $f(p)$ 表示将 $p$ 序列变换为有序序列最少要交换多少次,给一 $1 \sim n$ 的排列 $a$ ,给一整数 $m$, 求问将 $a$ 最少交换多少次能得到 $p$ ,使得 ...
- ORA-22992:没法使用从远程表选择的LOB定位器
OLB 问题 ORA-22992:没法使用从远程表选择的LOB定位器 Create global temporary table temp on commit preserve rows as sel ...
- JavaScript Symbol
创建: 2019/02/26 完成: 2019/02/26 生成 每次生成的值都不一样(===, ==都是) var sym = Symbol(); // 可以有参数, 是对symbol的说明 v ...
- 基于ndk_r7_windows编译实现ndk项目,不需要cygwin
下面就介绍下Android NDK的入门学习过程: 入门的最好办法就是学习Android自带的例子, 这里就通过学习Android的NDK自带的demo程序:hello-jni来达到这个目的. 一. ...
- 传统JDBC操作数据库
package com.jdbc.example; import java.sql.Connection; import java.sql.Date; import java.sql.DriverMa ...
- [Xcode 实际操作]八、网络与多线程-(18)PerformSelector消息处理方法:由运行时系统,负责去调用对象的指定方法
目录:[Swift]Xcode实际操作 本文将演示PerformSelector消息处理方法. 在项目文件夹上点击鼠标右键弹出文件菜单. [New File]->[Swift File]-> ...
- 16.join 用法(拼接列表时里面必须为str类型)
s1='alex' s2='+'.join(s1) print(s2,type(s2))#a+l+e+x <class 'str'> l1=['小红','小刚','小明'] 前提:列表中的 ...
- Pycharm2019.1.2永久激活
五月八日Pycharm更新至2019.1.2,小伙们是否也及时更新了呢?值得注意的是以前的激活方式已不适用于本次更新,这里分享最新的激活方法,有需要的同学请扫码关注我的公众号获取 重申:如果经济条件允 ...
- 转 java ClassLoader
http://blog.csdn.net/xyang81/article/details/7292380 http://www.ibm.com/developerworks/cn/java/j-lo- ...
- Java EE规范下载