bnu 34988 Happy Reversal

來源:互聯網
上載者:User

標籤:style   http   color   使用   os   io   for   ar   

Happy ReversalElfness 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? InputThe 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
25 61001000011000100010100011111115 700011010001011001001101110001001011
Sample Output
Case #1: 51Case #2: 103
Source2014 ACM-ICPC Beijing Invitational Programming Contest


題解及代碼:


#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>#include <cstring>using namespace std;long long b_2[63];void init(){    b_2[0]=1;    for(int i=1;i<=60;i++)    {        b_2[i]=2*b_2[i-1];    }}long long cal(char s[],int v){    long long ans=0;    int len=strlen(s);    int i=0;    for(int i=0;i<len;i++)    if(v==1&&s[i]=='1')    {        ans+=b_2[len-i-1];    }    else if(v==2&&s[i]=='0'){        ans+=b_2[len-i-1];    }    return ans;}struct node{    int id;    long long val;}sn[20010];bool cmp(node a,node b){    return a.val>b.val;}int main(){    init();    int cas,n,k;    char s[64];    memset(s,0,sizeof(s));    scanf("%d",&cas);    for(int ca=1;ca<=cas;ca++)    {        scanf("%d%d",&n,&k);        for(int i=0;i<n;i++)        {            scanf("%s",s);            sn[i*2].id=sn[i*2+1].id=i;            sn[i*2].val=cal(s,1);            sn[i*2+1].val=cal(s,2);        }        sort(sn,sn+2*n,cmp);        printf("Case #%d: ",ca);        if(n==1)        {            printf("0\n");            continue;        }        if(sn[0].id!=sn[2*n-1].id)        {            printf("%lld\n",sn[0].val-sn[2*n-1].val);        }        else        {            printf("%lld\n",max(sn[0].val-sn[2*n-2].val,sn[1].val-sn[2*n-1].val));        }    }    return 0;}/*我們只需要將每個數的本身及它的按位取反的數都求出來就行了。因為我們不能使用一個數與它的按位取反的數相減,所以我們對兩個出身相同的數進行編號。然後將所有的數進行排序,找到最大的數和最小的數,如果兩個數出身不同,那麼直接用大數減去小數就是答案了;如果兩數出身相同,那麼我們用第二大的數減去最小的數,與最大的數減去第二小的數進行比較,輸出更大者就行了。*/





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.