2014北京邀请赛 Happy Reversal
2014-05-21 16:39
302 查看
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; }
相关文章推荐
- ACM-ICPC 2014北京邀请赛 H Happy Reverse [模拟]
- HDU 5119 Happy Matt Friends(2014北京区域赛现场赛H题 裸背包DP)
- 2014北京邀请赛E题-矩阵快速幂
- 2014北京邀请赛E题_BNU_44673_Elegant String(DP+矩阵快速幂)
- hash(2014北京邀请赛)bnu34990
- 2014 北京邀请赛 B
- HDOJ5119 Happy Matt Friends(状压dp) ——2014北京现场赛H题
- 2014北京邀请赛 E Elegant String bunoj 34985 矩阵快速幂
- HDOJ5119 Happy Matt Friends(状压dp) ——2014北京现场赛H题
- 2014北京邀请赛
- 2014北京邀请赛 F Football on Table
- hdu 5119 Happy Matt Friends 2014 北京区域赛 递推
- 2014 北京邀请赛ABDHJ题解
- 2014ACM 北京邀请赛 B题
- 2014北京邀请赛B Beautiful Garden
- 2014北京邀请赛反思
- hihocoder 1084 扩展KMP && 2014 北京邀请赛 Justice String
- 2014北京邀请赛 F Football on Table
- ACM-ICPC2014北京邀请赛感受
- bnuoj34985 Elegant String 2014北京邀请赛E 递推+矩阵快速幂