首页 > 代码库 > 2014北京邀请赛 Happy Reversal
2014北京邀请赛 Happy Reversal
H. Happy Reversal
64-bit integer IO format: %lld Java class name: Main
Elfness is studying in an operation "NOT".
For a binary number A, if we do operation "NOT A", after that, all digits of A will be reversed. (e.g. A=1001101, after operation "NOT A", A will be 0110010).
Now Elfness has N binary numbers of length K, now he can do operations "NOT" for some of his numbers.
Let‘s assume after his operations, the maximum number is M, the minimum number is P. He wants to know what‘s the maximum M - P he can get. Can you help him?
Input
The first line of input is an integer T (T ≤ 60), indicating the number of cases.
For each case, the first line contains 2 integers N (1 ≤ N ≤ 10000) and K (1 ≤ K ≤ 60), the next N lines contains N binary numbers, one number per line, indicating the numbers that Elfness has. The length of each binary number is K.
Output
For each case, first output the case number as "Case #x: ", and x is the case number. Then you should output an integer, indicating the maximum result that Elfness can get.
Sample Input
2 5 6 100100 001100 010001 010001 111111 5 7 0001101 0001011 0010011 0111000 1001011
Sample Output
Case #1: 51 Case #2: 103
代码:
#include <stdio.h> #include <string.h> #include <limits.h> #define INF 0x7fffffffffffffffl int n, k; char bin[10005][65]; long long val[50005], cnt; void change(int x, int sign) { long long ret, r; ret = 0; r = 1; if (sign == 1){ for (int i = k - 1; i >= 0; i--){ ret += r*(bin[x][i] - '0'); r = r * 2; } } else { for (int i = k - 1; i >= 0; i--){ if (bin[x][i] == '0') ret += r; r = r * 2; } } val[cnt++] = ret; } int main() { int t, CASE = 1; scanf("%d", &t); while (t--) { scanf("%d%d", &n, &k); cnt = 0; for (int i = 0; i < n; i++){ scanf("%s", bin[i]); change(i, 1); change(i, -1); } long long max, min, s1, s2; max = val[0]; s1 = 0; for (int i = 1; i < 2 * n; i++) if (max < val[i]){ max = val[i]; s1 = i; } if (0 == s1 % 2) s2 = s1 + 1; else s2 = s1 - 1; min = INF; for (int i = 0; i < 2 * n; i++) if (i != s2&&min>val[i]) min = val[i]; printf("Case #%d: %lld\n", CASE++, max - min); } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。