【2017青岛网络赛】hdu 6212 Zuma 1007 区间dp
2017-09-18 20:34
495 查看
Problem Description
Think about the Zuma Game. You have a row of at most 200 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.
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 of 0 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
4
10101
101001001
1001001001
01001101011001100
Sample Output
Case #1: 4
Case #2: 3
Case #3: 3
Case #4: 2
题意:
给你一串01串,你可以在任意位置插入0或1,有三个及以上的连续的相同的会消去,会有连锁反应,问至少插入几次能消完
思路:听说是个原题,,,这场真多原题
有三种消除方式:
1.直接将区间分成两部分,各消各的。
2.如果两头是同色的,可以消完中间的,合并后消去两头,代价和两头的数量有关。
3.如果两头同色,又存在一头只有一个连续的情况,可以中间再找一个相同颜色的,三个合并后再消。
Think about the Zuma Game. You have a row of at most 200 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.
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 of 0 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
4
10101
101001001
1001001001
01001101011001100
Sample Output
Case #1: 4
Case #2: 3
Case #3: 3
Case #4: 2
题意:
给你一串01串,你可以在任意位置插入0或1,有三个及以上的连续的相同的会消去,会有连锁反应,问至少插入几次能消完
思路:听说是个原题,,,这场真多原题
有三种消除方式:
1.直接将区间分成两部分,各消各的。
2.如果两头是同色的,可以消完中间的,合并后消去两头,代价和两头的数量有关。
3.如果两头同色,又存在一头只有一个连续的情况,可以中间再找一个相同颜色的,三个合并后再消。
// // main.cpp // 1007 // // Created by zc on 2017/9/18. // Copyright © 2017年 1004. All rights reserved. // #include <iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int N=220; char s ; int a ,f ; int main(int argc, const char * argv[]) { int T,kase=0; scanf("%d",&T); while(T--) { scanf("%s",s); int n=strlen(s),m=0; a[++m]=1; for(int i=1;i<n;i++) { if(s[i]!=s[i-1]) a[++m]=0; a[m]++; } for(int i=m;i>0;i--) for(int j=i;j<=m;j++) { if(i==j)//i==j 直接返回需要补充的数量 { f[i][j]=3-a[i]; continue; } f[i][j]=n+n;//初始化最大值为2*n for(int k=i;k<j;k++) f[i][j]=min(f[i][j],f[i][k]+f[k+1][j]);//分成两半考虑 if((j-i)&1) continue;//如果间隔为奇数,则不是同种颜色 f[i][j]=min(f[i][j],f[i+1][j-1]+(a[i]+a[j]==2));//i和j同色,可以等中间消掉,如果两边都是1还得加上1的代价 if(a[i]+a[j]<4)//如果i和j存在有1,可以i,k,j三个同色的一起消 { for(int k=i+2;k<j;k+=2) if(a[k]==1) f[i][j]=min(f[i][j],f[i+1][k-1]+f[k+1][j-1]); } } printf("Case #%d: %d\n",++kase,f[1][m]); } }
相关文章推荐
- HDU 6212 Zuma 2017青岛网络赛 区间DP
- 2017 ACM-ICPC 亚洲区(青岛赛区)网络赛 HDU 6212 1007 Zuma (区间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)
- 【2017青岛网络赛】1004 hdu 6209 The Intersection 二分
- 2017乌鲁木齐网络赛D题 区间dp
- 【2016-沈阳赛区网络赛-I】区间DP(QSC and Master,hdu 5900)
- HDU 6212 Zuma ACM/ICPC 2017 Qingdao Online(区间dp)
- hdu 5900 QSC and Master(区间dp 沈阳网络赛)
- HDU - 6208 The Dominator of Strings (2017 ACM-ICPC 亚洲区 (青岛赛区) 网络赛 1003)
- hdu2017青岛网络赛Zuma(区间dp)
- 2017沈阳网络赛 1008 HDU 6201 transaction transaction transaction(树形dp)
- hdu 5884 Sort 2016ACM/ICPC青岛赛区网络赛1007
- HDU 6200 2017沈阳网络赛 树上区间更新,求和
- 【2017青岛网络赛】1010 hdu 6215 Brute Force Sorting 双向链表+队列
- HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛
- HDU - 6213 Chinese Zodiac (2017 ACM-ICPC 亚洲区 (青岛赛区) 网络赛 1008)
- 【2017青岛网络赛】1006 hdu 6211 Pythagoras 打表+本原勾股数
- HDU - 6214 Smallest Minimum Cut (2017 ACM-ICPC 亚洲区 (青岛赛区) 网络赛 1009)
- hdu 5900 QSC and Master -ICPC网络赛沈阳赛区-区间dp