zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    jdbc crud
    xcode升级10
    Java 多线程系列 CountDownLatch
    51小项目——使用proteus搭建简易的光照度计-(2)
    51小项目——使用proteus搭建简易的光照度计-(1)
    关于proteus中仿真STM32F103芯片的注意事项
    MOOC_TCP简述
    STM32F411RE片内资源
    左移、右移——极简
    初学51——中断
  • 原文地址:https://www.cnblogs.com/HarryGuo2012/p/4733117.html
Copyright © 2011-2022 走看看