zoukankan      html  css  js  c++  java
  • ZOJ3870 Team Formation【位运算+数学】


    Team Formation

    Time Limit: 2 Seconds      Memory Limit: 131072 KB

    For an upcoming programming contest, Edward, the headmaster of Marjar University, is forming a two-man team from N students of his university.

    Edward knows the skill level of each student. He has found that if two students with skill level A and B form a team, the skill level of the team will be A  B, where ⊕ means bitwise exclusive or. A team will play well if and only if the skill level of the team is greater than the skill level of each team member (i.e. A  B > max{A, B}).

    Edward wants to form a team that will play well in the contest. Please tell him the possible number of such teams. Two teams are considered different if there is at least one different team member.

    Input

    There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

    The first line contains an integer N (2 <= N <= 100000), which indicates the number of student. The next line contains N positive integers separated by spaces. Theith integer denotes the skill level of ith student. Every integer will not exceed 109.

    Output

    For each case, print the answer in one line.

    Sample Input

    2
    3
    1 2 3
    5
    1 2 3 4 5
    

    Sample Output

    1
    6
    

    Author: LIN, Xi
    Source: The 12th Zhejiang Provincial Collegiate Programming Contest


    问题链接ZOJ3870 Team Formation

    问题简述输入n个正整数,求任意两个数异或运算结果大于两个数中的最大值的组数。

    问题分析

    这里给出异或运算的规则,1^1=0,1^0=1,0^1=1,0^0=0。

    对于y而言,若y的第i位为1(从右到左分别为0-31位),y的第j位为0(j<i),

    对于第j位为1并且是最高位的1的x,

    那么y^x>y(按照假定满足x<y)。

    程序说明

    数组bit[],bit[i]表示各个数(所有的a)中第i位是最高位1的数的个数。

    数组left[],left[i]保存a[i]的为1的最高位。

    题记:(略)

    AC的C++语言程序如下:

    /* ZOJ3870 Team Formation */
    
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    
    const int N = 100000;
    const int N2 = 32;
    
    int a[N], left[N], bit[N2];
    
    void left1count(int x, int i) {
        int l = N2 - 1;
        left[i] = -1;
        while(l >= 0) {
            if(x & (1 << l)) {
                bit[l]++;
                left[i] = l;
                break;
            }
            l--;
        }
        return;
    }
    
    int main()
    {
        int t, n;
        scanf("%d", &t);
        while(t--) {
            memset(bit, 0, sizeof(bit));
            scanf("%d",&n);
            for(int i=0; i<n; i++) {
                scanf("%d", &a[i]);
                left1count(a[i], i);
            }
    
            long long ans = 0;
            for(int i=0; i<n; i++) {
                if(a[i]) {
                    while(left[i] >= 0) {
                        if(!(a[i] & (1 << left[i])))
                            ans += bit[left[i]];
                        left[i]--;
                    }
                }
            }
            printf("%lld
    ", ans);
        }
    
        return 0;
    }


  • 相关阅读:
    [03] html 中引入与使用css
    [04] 前端构建工具区别
    [04]测试框架杂谈
    [03] react 测试
    [02] 前端测试工具集锦
    [01]关于TDD、BDD和DDD的一些看法
    [04]JS获取文件大小方法
    [03]使用阿里RAP搭建前端Mock Server
    [1]区分event对象中的[clientX,offsetX,screenX,pageX]
    [02]a tag只为成button用时候设置href的办法
  • 原文地址:https://www.cnblogs.com/tigerisland/p/7563616.html
Copyright © 2011-2022 走看看