• AndyQsmart ACM学习历程——ZOJ3870 Team Formation(位运算)


    Description

    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 AB, 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. AB > 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. The ith 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
    

    这道题思路一次进入,直接1A。关键是抓住位运算的特点。对于亦或运算1和0运算的结果还是1,0和0运算的结果还是0,而1和1运算结果会变成0。所以对于一个二进制数A,比如1001001.那么对于B,不妨设A>B,要想让A^B > A,必然B中的第一位1要和A中的0进行亦或运算。所以对于一个二进制位固定长度的B,只要第一位1和A中的0进行亦或,那么答案一定是变大的。

    所以只需要枚举A中0的位置,然后找其二进制位对应长度的B的个数即可。而且预处理计算出二进制各长度的数的个数,并保存在cnt数组里,加速了计算。

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <set>
    #include <map>
    #include <queue>
    #include <string>
    #define LL long long
    
    using namespace std;
    
    bool cmp(int a, int b)
    {
        return a > b;
    }
    
    int n, a[100005], l[100005];
    int cnt[40];
    
    int GetLen(int n)
    {
        int len = 0;
        while (n)
        {
            len++;
            n >>= 1;
        }
        return len;
    }
    
    void Init()
    {
        memset(cnt, 0, sizeof(cnt));
        for (int i = 0; i < n; ++i)
        {
            l[i] = GetLen(a[i]);
            cnt[l[i]]++;
        }
    }
    
    LL Work()
    {
        int len;
        LL ans = 0;
        for (int i = 0; i < n; ++i)
        {
            len = l[i];
            int j;
            for (j = len-1; j >= 0; j--)
            {
                if ((a[i] & (1<<j)) == 0)
                {
                    ans += cnt[j+1];
                }
            }
        }
        return ans;
    }
    
    int main()
    {
        //freopen("test.in", "r", stdin);
        int T;
        scanf("%d", &T);
        for (int times = 0; times < T; ++times)
        {
            scanf("%d", &n);
            for (int i = 0; i < n; ++i)
                scanf("%d", &a[i]);
            sort(a, a+n, cmp);
            Init();
            printf("%lld
    ", Work());
        }
        return 0;
    }
    
  • 相关阅读:
    多路径下使用ASMLIB创建ASM磁盘
    linux7.4开启hugepages
    Oracle 12CR2 RAC 升级
    深度思考比勤奋更重要(转)
    Oracle最大保护模式是有延迟的
    mysql主从安装简记
    Socket 监控服务器运行状态
    12C Sharding 学习安装
    惊喜与局限并存,12c Sharding内测报告抢先看!
    Oracle 12c 分片(Sharding)技术
  • 原文地址:https://www.cnblogs.com/andyqsmart/p/4472424.html
Copyright © 2020-2023  润新知