lightoj-1422-Halloween Costumes 区间dp
来源:互联网 发布:淘宝卖家电话在哪里 编辑:程序博客网 时间:2024/06/06 13:12
1422 - Halloween Costumes
Gappu has a very busy weekend ahead of him. Because, nextweekend 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 alwaysselects 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 willgo 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 theHalloween night, and wear costumes accordingly, he will be changing hiscostumes a number of times. So, to make things a little easier, he may put oncostumes 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 thecostumes, or wear a new one. That is, if he is wearing the Postman uniform overthe Superman costume, and wants to go to a party in Superman costume, he cantake off the Postman uniform, or he can wear a new Superman uniform. But, keepin 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 theHalloween night, if he needs the Postman costume again, he will have to use anew one. He can take off any number of costumes, and if he takes offkof the costumes, that will be the last k ones (e.g. if he wears costumeAbefore costumeB, to take off A, first he has to removeB).
Given the parties and the costumes, find the minimum numberof 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 integerN (1≤ N ≤ 100) denoting the number of parties. Next line containsNintegers, where the ith integer ci (1 ≤ci ≤ 100) denotes the costume he will be wearing in partyi.He will attend party 1 first, then party 2, and so on.
Output
For each case, print the case number and the minimum numberof required costumes.
Sample Input
Output for Sample Input
2
4
1 2 1 2
7
1 2 1 1 3 2 1
Case 1: 3
Case 2: 4
对于区间[x, y), 如果有i使得a[x] == a[i], 则dp[x][y]=min(dp[x][i]+dp[i][y]-1, dp[x][y]), 否则dp[x][y]=dp[x][i]+dp[i][y]。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <cmath>#include <cstdlib>#include <vector>#include <queue>#include <stack>#include <set>#include <map>using namespace std;#define ll long long#define rep(x, y, z) for(int x=y;x<z;x++)const int maxn=1e2+7;int T;int n;int a[maxn];int dp[maxn][maxn];int kase;void init(){ memset(dp, 0, sizeof(dp));}void dfs(int x, int y){ if(y-x == 1) dp[x][y]=1; rep(i, x+1, y){ if(dp[x][i] == 0) dfs(x, i); if(dp[i][y] == 0) dfs(i, y); if(a[i] == a[x]){ if(dp[x][y] != 0) dp[x][y]=min(dp[x][y], dp[x][i]+dp[i][y]-1); else dp[x][y]=dp[x][i]+dp[i][y]-1; } if(dp[x][y] != 0) dp[x][y]=min(dp[x][y], dp[x][i]+dp[i][y]); else dp[x][y]=dp[x][i]+dp[i][y]; }}void solve(){ /* rep(i, 0, n){ if(dp[0][i] == 0) dfs(0, i); if(dp[i][n] == 0) dfs(i, n); if(dp[0][n] != 0) dp[0][n]=min(dp[0][n], dp[0][i]+dp[i][n]); else dp[0][n]=dp[0][i]+dp[i][n]; } */ dfs(0, n); printf("Case %d: %d\n", ++kase, dp[0][n]);}int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); scanf("%d", &T); while(T--){ init(); scanf("%d", &n); rep(i, 0, n){ scanf("%d", &a[i]); } solve(); } return 0;}
- 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)
- LightOJ 1422 Halloween Costumes (区间DP)
- 数据库小工具整合版
- 仿QQ空间图片放缩查看
- ADO.NET(OleDb)读取Excel表格时的一个BUG
- wireshark on mac :::no interface can be used for capturing in this system with the current configura
- #define宏中的“#”和“##”
- lightoj-1422-Halloween Costumes 区间dp
- 哈尔小波变换实例分析
- 服务器 Server installshield脚本
- HDOJ题目2196 Computer(树的直径)
- Exploid:CVE-2009-1185 init未检查NETLINK消息来源,本地提权漏洞
- VS2008到VS2010转换到 COFF 期间失败
- Android开发中EditText小技巧之如何设置光标颜色及粗细
- Android Fragment 生命周
- android 读取 AndroidManifest.xml 中的数据:版本号、应用名称、自定义K-V数据(meta-data)