标签: 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第一题的更多相关文章

  1. ACM学习历程—HDU1584 蜘蛛牌(动态规划 && 状态压缩 || 区间DP)

    Description 蜘蛛牌是windows xp操作系统自带的一款纸牌游戏,游戏规则是这样的:只能将牌拖到比她大一的牌上面(A最小,K最大),如果拖动的牌上有按顺序排好的牌时,那么这些牌也跟着一起 ...

  2. poj 2955 Brackets (区间dp基础题)

    We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a ...

  3. 又一道区间DP的题 -- P3146 [USACO16OPEN]248

    https://www.luogu.org/problemnew/show/P3146 一道区间dp的题,以区间长度为阶段; 但由于要处理相邻的问题,就变得有点麻烦; 最开始想了一个我知道有漏洞的方程 ...

  4. 状态压缩dp第一题

    标签: ACM 题目: Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; ...

  5. 状态压缩---状态压缩dp第一题

    标签: ACM 题目: Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; ...

  6. POJ 2441 Arrange the Bulls 状态压缩递推简单题 (状态压缩DP)

    推荐网址,下面是别人的解题报告: http://www.cnblogs.com/chasetheexcellence/archive/2012/04/16/poj2441.html 里面有状态压缩论文 ...

  7. B1068 [SCOI2007]压缩 区间dp

    这个题我状态想对了,但是转移错了...dp的代码难度都不大,但是思考含量太高了..不会啊,我太菜了. 其实这个题就是一个正常的区间dp,中间多了一个特判的转移就行了. 题干: Description ...

  8. 【BZOJ-1068】压缩 区间DP

    1068: [SCOI2007]压缩 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1001  Solved: 615[Submit][Status][ ...

  9. hdu 4649 Professor Tian 反状态压缩+概率DP

    思路:反状态压缩——把数据转换成20位的01来进行运算 因为只有20位,而且&,|,^都不会进位,那么一位一位地看,每一位不是0就是1,这样求出每一位是1的概率,再乘以该位的十进制数,累加,就 ...

随机推荐

  1. C - Present

    C - Present Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit ...

  2. Tomcat自定义classLoader加密解密

    class很好反编译,所以需要对class文件先进行加密,然后使用自己的classloader进行解密并加载. [步骤] 大概分两步: 1.对class文件进行加密 2.写解密class文件并加载的c ...

  3. eclipse + tomcat 开发环境配置

    一. 下载tomcat和Eclipse 下载tomcat 下载地址:http://tomcat.apache.org/download-70.cgi 下载后解压如下图 下载eclipse 下载地址:h ...

  4. PostgreSQL 务实应用(一/5)树形层级

    项目中,经常会碰到多级的树形结构数据,如地区信息,省.市.区.街道等,或客户关系信息上三级,下三级等. 实际项目中,我们可能碰到以下两种需求: 一条记录中呈现路径:省 - 市 - 区 - 街道 一条记 ...

  5. 洛谷 - P1891 - 疯狂LCM - 线性筛

    另一道数据范围不一样的题:https://www.cnblogs.com/Yinku/p/10987912.html $F(n)=\sum\limits_{i=1}^{n} lcm(i,n) $ $\ ...

  6. rsync 同步的艺术

    rsync是类unix系统下的数据镜像备份工具,可以提供快速的增量文件传输. rsync 也可用于本机传输,如: # rsync -v mysql--linux-glibc2.-x86_64.tar. ...

  7. 基础篇-psql帮助命令

    \? psql命令帮助 \h sql语句帮助 \?常用命令 1.默认  \d 后面不跟参数,则显示当前数据库所有的表 2. \d  表名  ,则显示这个表的定义 3.\d 索引名  ,显示索引的信息 ...

  8. Mybatis源码解析(二)

    根据上篇的代码跟踪mybatis已经ready好 SqlSessionFactory了,下面就是我们怎么去通过这个factory去获取sqlSession会话了,继续扒源码: mybatis-spri ...

  9. DRF教程2-请求和响应

    Request objects REST framework中有一个Request对象,是HttpRequest的扩展,提供了新的请求解析,Request的核心功能就是request.data,它和r ...

  10. 关于yii2自带验证码功能不显示问题

    1,验证码不显示: 首先保证你的controler 里面的captcha方法是可访问的,被分配的权限的,这个在rule里面设置. 第二,保证你的PHP GD插件已经被启用, 第三如果这样还是不显示,那 ...