zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    以太坊DApp开发(2):以太坊智能合约开发环境搭建以及第一个Dapp
    以太坊DApp开发(1):入门-开发环境搭建
    全文搜索引擎 Elasticsearch (四)MySQL如何实时同步数据到ES
    nginx 添加https 配置
    spring boot 2.0.3+spring cloud (Finchley)6、配置中心Spring Cloud Config
    linux中没有dos2UNIX或者UNIX2dos命令解决办法
    linux 安装redis4.0
    maven 不同环境打包命令
    PowerShell 中使用 mvn 编译报错 Unknown lifecycle phase ".test.skip=true". 解决办法
    通过更改scrapy源码进行spider分发实现一个综合爬虫
  • 原文地址:https://www.cnblogs.com/andyqsmart/p/4472424.html
Copyright © 2011-2022 走看看