H. Happy Reversal
Time Limit: 1000ms
Memory Limit: 65536KB
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
就是很简单把原字符串和NOT后的字符串转成十进制之后存在一数组里,然后简单的排个序,取最大和最小,然后遇到最大最小是一个字符串的时候,找第二大和最小以及最大和第二小,然后比一比就完事啦
#include<stdio.h> #include<algorithm> using namespace std; char a[10000][1000]; char b[10000][1000]; long long int s[100000]; int main() { int t,n,m,i,j,k; scanf("%d",&t); int g=1; while(t--) { int h=0; scanf("%d %d",&n,&m); for(i=0; i<n; i++) { scanf("%s",a[i]); } int y=0; for(i=0; i<n; i++) { long long int sum=0; int l=1; for(j=m-1; j>=0; j--) { long long int p=1; for(k=0; k<l-1; k++) { p=p*2; } sum+=(a[i][j]-'0')*p; l++; } s[y++]=sum; } for(i=0; i<n; i++) { for(j=0; j<m; j++) { if(a[i][j]=='0') { b[i][j]='1'; } if(a[i][j]=='1') { b[i][j]='0'; } } } for(i=0; i<n; i++) { long long int sum=0; int l=1; for(j=m-1; j>=0; j--) { long long int p=1; for(k=0; k<l-1; k++) { p=p*2; } sum+=(b[i][j]-'0')*p; l++; } s[y++]=sum; } sort(s,s+y); long long s1=s[y-1]-s[1]; long long s2=s[y-2]-s[0]; long long p=s[0],q=s[y-1]; long long e=1<<(m-1);//判断最大的和最小的是否是同一个字符串 if((p^q)&e==e)//遇到最大最小是一个字符串的时候,找第二大和最小以及最大和第二小,然后比一比就完事啦 printf("Case #%d: %lld\n",g++,max(s1,s2)); else printf("Case #%d: %lld\n",q-p); } }