状态压缩---区间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的概率,再乘以该位的十进制数,累加,就 ...
随机推荐
- 使用httpClient下载网页
HttpCore 对HTTP协议客户端编程做了一些基本的封装.例如,格式化请求头和解析响应头.LineF ormatter用来格式化请求头信息,而实际的实现在BasicLineF ormatter 上 ...
- c++中编译链接总结
1 编译链接过程分为 预处理--->编译---->汇编---->链接.如下图所示 2 预处理都做了什么 (1)将所有的#define删除并展开所有的宏 (2)处理所有的条件预编译指令 ...
- 技术胖Flutter第三季-16Stack层叠布局
16Stack层叠布局 在上面声明一个变量Stack里面包含两个元素,第一个 是CircleAvater第二个子对象是Container 效果 把文字房子啊中下的位置: 我们需要对齐属性 包含了x轴和 ...
- jQuery 学习笔记(一)jQuery 语法
jQuery 是一个 JavaScript 库,极大地简化了 JavaScript 编程,很容易学习 添加 jQuery 库 <head> <script type="te ...
- 纯css单选框
1.没有用bootstrap时: .has_sel,.un_sel{display:block; width:16px; height: 16px; border: 1px solid #B06A50 ...
- STLstack,queue
今天一开始用C去摸栈和队列,差不多昨天早上也在摸,摸烦了就去搞DP然后DP也没搞好,就是很烦很烦!!!! 然后今天那些C的栈队列的步骤和名称熟的不要不要的,然而数据结构的c语言用指针,传递,简直麻烦, ...
- 【Aizu - ALDS1_7_A】Rooted Trees(树的表达)
Rooted Trees Descriptions: A graph G = (V, E) is a data structure where V is a finite set of vertice ...
- 黑马Stream流学习 Stream流 函数式接口 Lambda表达式 方法引用
- JAVA列出某文件夹下的所有文件
import java.io.*; public class ListFiles { private static String s = ""; private static Bu ...
- js实现打印正三角
代码: <html> <head> <title>function</title> </head> <body> <scr ...