Halloween Costumes+lightOJ+区间dp
来源:互联网 发布:u盘文件数据恢复 编辑:程序博客网 时间:2024/05/18 01:08
Description
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[j][i]代表区间j到i穿的衣服最少的件数。但怎样求最少衣服件数呢?首先i有两种选择穿或不穿,若穿,则:dp[j][i]=dp[j][i-1]+1,不穿的情况是在j到i-1之间有和i衣服一样的衣服,则:dp[j][i]=dp[j][k-1]+dp[k][i-1];
code:
<pre name="code" class="cpp">#include <iostream>#include<cstdio>#include<cstring>using namespace std;int dp[103][103];int a[103];int main(){ int t,kd=0; scanf("%d",&t); while(t--){ int N; scanf("%d",&N); for(int i=1;i<=N;i++){ scanf("%d",&a[i]); } memset(dp,0,sizeof(dp)); for(int i=1;i<=N;i++){ dp[i][i]=1; } for(int i=1;i<=N;i++){ for(int j=1;j<=i-1;j++){ dp[j][i]=dp[j][i-1]+1; for(int k=j;k<=i-1;k++){ if(a[k]==a[i]) dp[j][i]=min(dp[j][i],dp[j][k-1]+dp[k][i-1]); } } } printf("Case %d: %d\n",++kd,dp[1][N]); } return 0;}
- Halloween Costumes+lightOJ+区间dp
- Lightoj 1422 Halloween Costumes(区间DP)
- LightOJ 1422 Halloween Costumes 区间DP
- LightOJ - 1422 Halloween Costumes (区间DP)
- LightOJ 1422 Halloween Costumes(区间dp)
- LightOJ 1422 Halloween Costumes(区间DP)
- LightOJ 1422 - Halloween Costumes(区间DP)
- 区间dp LightOj 1422 Halloween Costumes
- lightoj 1422Halloween Costumes(区间dp)
- LightOJ 1422 Halloween Costumes 【区间DP】
- LightOJ 1422 Halloween Costumes (区间dp)
- LightOJ 1422 Halloween Costumes (区间DP)
- lightoj-1422-Halloween Costumes 区间dp
- LightOJ - 1422 Halloween Costumes 区间DP
- LightOJ 1422 Halloween Costumes(区间DP )
- lightoj 1422 - Halloween Costumes 【区间dp入门】
- lightoj 1422 - Halloween Costumes(区间DP)
- LightOj 1422 Halloween Costumes(区间DP)
- 纯虚函数和抽象类
- 啊上课的话卡的是哪个啊客户订购
- 可得网-体验式营销在互联网营销中的应用
- 分享一个简单易用的RPC开源项目—Tatala
- openwrt 编译 gcc 包package
- Halloween Costumes+lightOJ+区间dp
- 深入浅出Swift(1)——基础语法
- 初识UITableView
- 按位与、或、异或等运算方法
- 网页实现批量数据导入功能
- 2、ExtJs——ExtJs入门
- Java程序员从笨鸟到菜鸟之(六十七)细谈Spring(一)spring简介
- iPhone 6 与 iPhone 6 Plus 参数对比
- HDU_1824_Let's go home(2-SAT)