UVa12283 区间dp
来源:互联网 发布:在线订餐系统源码asp 编辑:程序博客网 时间:2024/06/05 03:19
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 contestbuddies, 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
First line contains T (T2500), the number of test cases.
Each test case starts with two integers, N and M (1N, M100), the number of parties, and number of different types of costumes. Next line contains N integers, ci (1ciM), the costume he will be wearing in party i. He will attend the party 1 first, then party 2, and so on.
There is a blank line before each test case.
Output
For each test case, output the minimum number of required costumes. Look at the output for sample input for details.
Sample Input
4 1 1 1 2 2 1 1 3 2 1 2 1 7 3 1 2 1 1 3 2 1
Sample Output
Case 1: 1Case 2: 1Case 3: 2Case 4: 4
这题在UVa中wa了三发,主要是oj不能识别PE,最后少写了一个换行。
这题只能用二维的dp来写,因为涉及到了区间的状态,所以应该用区间dp来写。
这里还用到了去重的函数n=unique(a+1,a+1+n)-a;
dp[i][j]表示[i,j]最少要穿多少的衣服。dp[i][j]=min(dp[i][k]+dp[k+1][j]-val) if(a[i]==a[k+1]) val=1,else val=0;
代码:
#include<iostream>#include<algorithm>#include<vector>#include<string>#include<queue>#include<map>#include<cstdio>#include<cstring>#define maxn 200#define INF 0xfffffffusing namespace std;int n,m;int a[maxn],dp[maxn][maxn];void init(){ for(int i=1;i<=n;i++) dp[i][i]=1; for(int i=1;i<=n;i++) { for(int j=i+1;j<=n;j++) { dp[i][j]=INF; } }}int solve(){ int s,e; for(int len=1;len<n;len++) { for(int i=1;i+len<=n;i++) { s=i,e=i+len; for(int k=s;k<e;k++) { int tp=dp[s][k]+dp[k+1][e]; if(a[s]==a[k+1]) { tp--; } dp[s][e]=min(dp[s][e],tp); } } } return dp[1][n];}int main(){ int t; cin>>t; for(int tt=1;tt<=t;tt++) { cin>>n>>m; for(int i=1;i<=n;i++) { cin>>a[i]; } n = unique(a+1, a + n+1) - a-1;//去掉连续的 init(); printf("Case %d: %d\n",tt,solve()); } return 0;}
- UVa12283 区间dp
- 区间DP
- 区间DP
- 区间DP
- 区间DP
- ##区间dp##
- 区间dp
- 区间DP
- 区间dp
- 区间dp
- 区间dp
- 区间dp
- 区间dp
- 区间dp
- 区间dp
- 区间DP
- 区间DP
- 区间DP
- ENVI扩展工具:航拍影像坏点修复专用补丁
- 常用的排序算法的时间复杂度和空间复杂度
- Android系统开发编译环境配置 (感觉好经典的系列)(其三)
- 【xinfanqie】笔者支招:让Win7系统下的硬盘不在狂闪的诀窍
- Facade门面模式
- UVa12283 区间dp
- Protel DXP布线规则设置
- Eclipse中java文件和jsp字体大小设置
- Android系统开发编译环境配置 (感觉好经典的系列)(其四)
- ural 1864. Get-Together at Den's (坑爹浮点数)
- [ oracle ] oracle之路4---存储过程!
- spring JDBCTemplete
- Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/core"
- android屏蔽返回键和home键等各个按键的方法