H. Happy Reversal

H. Happy Reversal

Time Limit: 1000ms
Memory Limit: 65536KB
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
題目意思是有多個01串,可以選擇反轉或者不反轉,求出最大值與最小值的最大差
看完題目後,直接暴力,提交後毫無懸念的超時了,這個題的關鍵是怎麼找到最大值與最小值,因爲
一個串你只能選擇一個值,但又不能保證選擇一個值得情況下,就是最好的結果,想了好久也沒
想明白,看了別人的代碼後,才明白,最大值或最小值是由二進制得來的,應該用異或來解決
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

int n,m,h;
char str[100];
long long a[20010];

long long pow(int k){
    return 1LL<<k;
}

long long solve(){
    if(n==1)
        return 0;
    if(a[0]^a[h-1])
        return max(a[h-2]-a[0],a[h-1]-a[1]);
    else
        return a[h-1]-a[0];
}

int main(){
    long long Max,p,q;
    int i,j,k,t,l=1;
    scanf("%d",&t);
    while(t--){
        h=0;
        scanf("%d%d",&n,&m);
        for(k=0;k<n;k++){
            scanf("%s",str);
            p=0;
            q=0;
            for(i=m-1,j=0;i>=0;i--,j++){
                if(str[i]=='0') p+=pow(j);
                else q+=pow(j);
            }
            a[h++]=p;
            a[h++]=q;
        }
        sort(a,a+h);
        printf("Case #%d: %lld\n",l++,solve());
    }
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章