Hdu-6212 Zuma(区间DP)
来源:互联网 发布:mac直播音效软件下载 编辑:程序博客网 时间:2024/06/08 19:24
Think about the Zuma Game. You have a row of at most 200 Input The first line of input contains an integer T (1≤T≤100) which is the total number of test cases.
Each test case contains a line with a non-empty string of0 and 1 describing the row of balls at the start. Output For each test case, output the case number and the minimal balls required to insert in a line. Sample Input Sample Output
black(0) or white(1) balls on the table at the start. Each three consecutive balls never share the same colour. You also have infinite amount of black and white balls in your hand. On each turn, you can choose a ball in your hand and insert it into the row, including the leftmost place and the rightmost place. Then, if there is a group of three of more balls in the same colour touching, remove these balls. Keep doing this until no more balls can be removed.
Find the minimal balls you have to insert to remove all the balls on the table.
Find the minimal balls you have to insert to remove all the balls on the table.
Each test case contains a line with a non-empty string of
410101101001001100100100101001101011001100
Case #1: 4Case #2: 3Case #3: 3Case #4: 2
分析:我们把连续的小段用二元组表示后(001001 就可以表示成2121),设f[i][j]表示把i到j这段区间全部消除的最小代价,那么我们枚举最后一块j和谁一起消除来进行转移,这样就是
f[i][j] = min(f[i][k-1]+f[k][j]),边界情况是i和j一起消除,这时i和j中间还可以最多夹杂两块1一起消除,但是我的程序只写了中间夹着一个1的情况也ac了,可能是数据弱或者夹着两个1的情况都可以用夹着一个1的情况代替?
#include <bits/stdc++.h>using namespace std;const int INF = 1e9 + 9;int T,n,d[205],f[205][205],g[205][205][2];char str[205];int main(){scanf("%d",&T);for(int t = 1;t <= T;t++){n = 0;scanf("%s",str);int len = strlen(str);int i = 0;while(i < len){if(i == len-1 || str[i+1] != str[i]){d[++n] = 1;i++;}else {d[++n] = 2;i+=2;}} for(int i = 1;i <= n;i++) f[i][i] = d[i] == 1 ? 2 : 1;for(int l = 2;l <= n;l++) for(int i = 1;i+l-1 <= n;i++) { int j = i + l - 1; f[i][j] = INF; for(int k = i+1;k <= j;k++) f[i][j] = min(f[i][j],f[i][k-1] + f[k][j]); if((l-1) & 1) continue; if(d[i] + d[j] >= 3) f[i][j] = min(f[i][j],f[i+1][j-1]); else f[i][j] = min(f[i][j],f[i+1][j-1] + 1); if(d[i] + d[j] == 4) continue; for(int k = i+1;k < j;k++) if(!((j-k) & 1) && d[k] == 1) f[i][j] = min(f[i][j],f[i+1][k-1] + f[k+1][j-1]); }cout<<"Case #"<<t<<": "<<f[1][n]<<endl;}}
阅读全文
0 0
- Hdu-6212 Zuma(区间DP)
- Hdu 6212 Zuma 区间DP
- Hdu 6212 Zuma【思维+区间Dp】
- 【2017青岛网络赛】hdu 6212 Zuma 1007 区间dp
- Codeforces_607B:Zuma(区间DP)
- HDU 6212 Zuma ACM/ICPC 2017 Qingdao Online(区间dp)
- 2017 ACM/ICPC Asia Regional Qingdao Online 1007 hdu 6212 Zuma 区间dp
- 2017 ACM/ICPC Asia Regional Qingdao Online 1007 hdu 6212 Zuma (区间dp)
- SPOJ-ZUMA(区间DP)
- CF607B: Zuma(区间dp)
- HDU6212 Zuma(区间dp)
- 2017 ACM-ICPC 亚洲区(青岛赛区)网络赛 HDU 6212 1007 Zuma (区间DP)
- SPOJ 6340 ZUMA(区间DP)
- Codeforces 607B - Zuma (区间DP)
- Codeforces 607B Zuma 【区间dp】
- codeforces 607B Zuma 区间dp
- codeforces #336 D. Zuma (区间dp)
- CodeForces 607 B. Zuma(区间DP)
- 人工智能实战2——从0开始手写符号识别
- 【c语言小项目】简单计算器
- c++指针【C++】
- Java泛型编程
- React-Native之Hello World
- Hdu-6212 Zuma(区间DP)
- 学渣课堂笔记--编译原理(1)
- 【ORACLE】手工建库+RMAN Catalog创建、配置和使用
- gpio子系统和pinctrl子系统(上)
- 最大最小字符串问题
- php链接数据库出现乱码
- OpenSim教程一——OpenSim 入门介绍
- 菜鸟note:数据库进阶之路(一)之保存用户名密码
- POJ 3694 Network(tarjan+lca)