Codeforces Gym 100203E bits-Equalizer 贪心

原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf

题解 

考虑到交换可以减少一次操作,那么可以的话就尽量交换,设问号的个数是z,上面是1下面是0的个数是x,反过来的是y,那么最后的答案就是z+min(x,y)+abs(x-y)。代码是队友写的,我是嘴炮流选手。

代码

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <queue>
#include <ctime>
#include <cmath>
#include <set>
#include <stack>
#include <map>
#define eps 1e-10
#define MAXN 500010
#define INF 2*0x3f3f3f3f
using namespace std;

int T;
char str1[110], str2[110];

int main() {
    //freopen("in.in", "r", stdin);
    //freopen("out.out", "w", stdout);
    scanf("%d", &T);
    for (int cas = 1; cas <= T; cas++) {
        scanf("%s %s", str1, str2);
        int len = strlen(str1);
        int x = 0, y = 0, z = 0;
        for (int i = 0; i < len; i++) if (str1[i] != '1') x++;
        for (int i = 0; i < len; i++) if (str2[i] != '1') y++;
        if (x < y) printf("Case %d: %d
", cas, -1);
        else {
            x = y = z = 0;
            for (int i = 0; i < len; i++) {
                if (str1[i] == '?') z++;
                else if (str1[i] == '0' && str2[i] == '1') x++;
                else if (str1[i] == '1' && str2[i] == '0') y++;
            }
            printf("Case %d: %d
", cas, z + min(x, y) + abs(y - x));
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/HarryGuo2012/p/4733117.html