Codeforces Gym 100203E bits-Equalizer 贪心
2015-08-15 21:12
429 查看
原题链接: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\n", 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\n", cas, z + min(x, y) + abs(y - x)); } } return 0; }
相关文章推荐
- A题之找K个最小的数
- Spring MVC常用的注解
- 插入排序
- 利用辛达拉姆筛进行素数判定
- C++ 2048简化版
- ios日期格式转换
- Xcode个人常用插件
- Scala入门到精通——第二十七节 Scala操纵XML
- junit基础学习之-简介(1)
- JAVA基础(三)
- 基本算法复习之排序:性能比较、代码分析
- 2.5 寻找最大的k个数
- Django之第一个app<1>
- LeetCode(59)Length of Last Word
- HDU5391-Zball in Tina Town
- 宁波Uber优步司机奖励政策(8月10号-16号)
- 两种计算和输出n内5要么9除尽互惠
- STL库函数 持续更新
- LeetCode(59)Length of Last Word
- Windows 10 IoT Core 正式版进阶体验