UVA 11920 - 0 s, 1 s and ? Marks(贪心)
来源:互联网 发布:软件的意思 编辑:程序博客网 时间:2024/05/18 14:42
0 s, 1 s and ? Marks
Given a string consisting of 0, 1 and ? only, change all the ? to 0/1, so that the size of the largest group is minimized. A group is a substring that contains either all zeros or all ones.
Consider the following example:
0 1 1 ? 0 1 0 ? ? ?
We can replace the question marks (?) to get
0 1 1 0 0 1 0 1 0 0
The groups are (0) (1 1) (0 0) (1) (0) (1) (0 0) and the corresponding sizes are 1, 2, 2, 1, 1, 1, 2. That means the above replacement would give us a maximum group size of 2. In fact, of all the 24 possible replacements, we won't get any maximum group size that is smaller than 2.
Input
The first line of input is an integer T ( T5000) that indicates the number of test cases. Each case is a line consisting of a string that contains 0, 1 and ? only. The length of the string will be in the range [1,1000].
Output
For each case, output the case number first followed by the size of the minimized largest group.Sample Input
4011?010??????00011100000000000000
Sample Output
Case 1: 2Case 2: 1Case 3: 3Case 4: 14
题意:给定一个串有0,1,?。?可以变成0或1,要求构造出一个串,使得连续的0,1的最大值最小。
思路:贪心,把连续的0,1,?保存在一起,然后去判断,如果是0,1就直接算最大值,如果是?,就看前面和后面是0还是1,如果前后相同且?个数为奇数或前后不同?个数为偶数,那么肯定可以交替01是最小的,反之,就看前面一串多1还是后面一串多1或者中间串多1比较好,注意如果中间只有一个?,那么就不用考虑中间多1的情况。
代码:
#include <stdio.h>#include <string.h>#define max(a,b) (a)>(b)?(a):(b)const int N = 1005;int t, pn;char str[N];struct Point{int v, num;} p[N];void init() {scanf("%s", str);memset(p, 0, sizeof(p));int len = strlen(str); pn = 0;char c = '#';for (int i = 0; i < len; i++) {if (str[i] == c) {p[pn - 1].num++;}else {c = str[i];p[pn].v = str[i] - '0';p[pn++].num++;}}}int solve() {int ans = 1;for (int i = 0; i < pn; i++) {if (p[i].v == 0 || p[i].v == 1) {ans = max(ans, p[i].num);}else {if (ans >= 2 && p[i].num != 1) continue;if (i == 0 || i == pn - 1) continue;if (p[i - 1].v == p[i + 1].v && p[i].num % 2) continue;if (p[i - 1].v != p[i + 1].v && p[i].num % 2 == 0) continue;if (p[i - 1].num <= p[i + 1].num) {p[i - 1].num++;ans = max(ans, p[i - 1].num);}else {p[i + 1].num++;ans = max(ans, p[i + 1].num);}}}return ans;}int main() {int cas = 0;scanf("%d", &t);while (t--) {init();printf("Case %d: %d\n", ++cas, solve());}return 0;}
- UVA 11920 - 0 s, 1 s and ? Marks(贪心)
- uva 11920 - 0 s, 1 s and ? Marks(贪心)
- uva 11920 0 s, 1 s and ? Marks
- UVA - 11920 0 s, 1 s and ? Marks
- UVa 11920 - 0 s, 1 s and ? Marks (二分 + DP)
- uva - 10026 - Shoemaker's Problem(贪心)
- UVa 10905 Children's Game ( 贪心 )
- uva-10905Children's Game(贪心)
- UVA - 10905 Children's Game(贪心)
- UVA - 10026 Shoemaker's Problem(贪心)
- UVA - 10041 Vito's Family(贪心)
- uva 434 - Matty's Blocks(贪心)
- UVA 10905 Children's Game (贪心)
- uva 10026 Shoemaker's Problem (贪心)
- UVa 10905 Children's Game (贪心)
- UVA Children’s Game(贪心)
- uva - 10905 - Children's Game(贪心,qsort排序)
- UVA 1420 Priest John's Busiest Day(贪心)
- SVN服务器搭建和使用(二)
- SWT
- C# FileStream和StreamWriter用法
- CentOS 6.5上使用gdb调试时出现Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.i686
- U盘安装Ubuntu 12.04 Server版提示“Faild to copy file from CD-ROM”的解决办法
- UVA 11920 - 0 s, 1 s and ? Marks(贪心)
- .Net琐碎知识点
- 两栏流动式布局
- Global Contrast based Salient Region Detection
- 优点汇总最自豪的事情“一”
- yii 模型
- cocos2d-x 通过python模板方式新建支持lua的工程
- Makefile选项CFLAGS,LDFLAGS,LIBS
- Java内部类的使用小结