[UVA][二分greedy] 11920 - 0 s, 1 s and ? Marks@Morris' Blog|PChome Online 人新台
2013-08-11 22:27:36| 人1,448| 回0 | 上一篇 | 下一篇

[UVA][二分greedy] 11920 - 0 s, 1 s and ? Marks

0 收藏 0 0 站台


  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 ( T$ le$5000) 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 

4 011?010??? ??? 000111 00000000000000 

Sample Output 

Case 1: 2 Case 2: 1 Case 3: 3 Case 4: 14 



使用的,事上也可以出的。

最特的是:

*0?1*     -> *0?1*      still don't know

因此考一的情且前後接的不同,二分答案後,
可能 '?' 於前面的那,否只好放後面。



#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
int greedy(int mx, char s[], int v[], int n) {
    int i, ret = 0;
    int prev = 0;
    for(i = 0; i < n; i++) {
        if(prev > mx)   return 0;
        if(s[i] == '?') {
            if(i && i != n-1 && s[i-1] != s[i+1]) {
                if(v[i] == 1) {
                    if(prev < mx) {
                        pre++;
                        prev = 0;
                    } else {
                        prev = 1;
                    }
                } else {
                    prev = 0;
                }
                if(v[i]&1)
                    ret = max(ret, 2);
                else
                    ret = max(ret, 1);
            } else if(i && i != n-1 && s[i-1] == s[i+1]) {
                if(v[i]&1)
                    ret = max(ret, 1);
                else
                    ret = max(ret, 2);
                prev = 0;
            } else {
            }
        } else {
            if(i && s[i-1] != '?')
                prev = 0;
            prev += v[i];
        }
        ret = max(ret, prev);
    }
    return ret <= mx;
}
int main() {
    int testcase, cases = 0;
    char s[1005];
    int v[1005];
    scanf("%d", &testcase);
    while(testcase--) {
        scanf("%s", s);
        int n = strlen(s);
        int l = 1, r = n, m;
        int ret = 1;
        int i, j, k;
        m = 0;
        for(i = 0; i < n; i++) {
            s[m] = s[i], v[m] = 1;
            while(i < n && s[i+1] == s[i])
                i++, v[m]++;
            m++;
        }
        n = m;
        /*for(i = 0; i < n; i++)
            printf("%c %d\n", s[i], v[i]);*/
        while(l <= r) {
            m = (l+r)/2;
            if(greedy(m, s, v, n))
                r = m-1, ret = m;
            else
                l = m+1;
        }
        printf("Case %d: %d\n", ++cases, ret);
    }
    return 0;
}
/*
 * if the digits before && after "?" is different, we can change the sequence to
 * *0?1*     -> *0?1*      still don't know
 * *0??1*    -> *0101*     ans>=1
 * *0???1*   -> *01001*    ans>=2
 * *0????1*  -> *010101*   ans>=1
 * ....
 * else if they are the same, "0" is consider here, the same to "1"
 * *0?0*     -> *010*      ans>=1
 * *0??0*    -> *0110*     ans>=2
 * *0???0*   -> *01010*    ans>=1
 * *0????0*  -> *010110*   ans>=2
*/

台: Morris
人(1,448) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 教育(修、留、研究、教育概) | 人分: UVA |
此分下一篇:[UVA][greedy] 11166 - Power Signs
此分上一篇:[UVA] 10906 - Strange Integration

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86