2014 ACM-ICPC Beijing Invitational Programming Contest

点击打开链接

Happy Reversal

1000ms
65536KB
64-bit integer IO format:  %lld      Java class name:  Main
Font Size:   
Type: 
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •                    
  • 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
    

    Source

    给你n组由k个0或者1组成的二进制数。每个数可以翻转,求两个数的最大差值。

    //152 ms 1788 KB
    #include<stdio.h>
    #include<algorithm>
    using namespace std;
    char s[107];
    long long ans[20007];
    int n,m;
    long long getnum()
    {
        long long res=0,j=1;
        for(int i=m-1;i>=0;i--,j*=2)
            if(s[i]=='1')res+=j;
        return res;
    }
    int main()
    {
        int t,cas=1;
        scanf("%d",&t);
        while(t--)
        {
            int k=0;
            scanf("%d%d",&n,&m);
            for(int i=0;i<n;i++)
            {
                scanf("%s",s);
                ans[k++]=getnum();
                for(int j=0;j<m;j++)
                    if(s[j]=='1')s[j]='0';
                    else s[j]='1';
                ans[k++]=getnum();
            }
            sort(ans,ans+k);
            long long ans1=ans[k-1]-ans[1];
            long long ans2=ans[k-2]-ans[0];
            long long p=ans[0],q=ans[k-1];
            long long e=1<<(m-1);
            if((p^q)&e==e)
                printf("Case #%d: %lld\n",cas++,max(ans1,ans2));
            else printf("Case #%d: %lld\n",q-p);
        }
        return 0;
    }


    • 0
      点赞
    • 0
      收藏
      觉得还不错? 一键收藏
    • 0
      评论

    “相关推荐”对你有帮助么?

    • 非常没帮助
    • 没帮助
    • 一般
    • 有帮助
    • 非常有帮助
    提交
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值