zoukankan      html  css  js  c++  java
  • ZOJ 3870 Team Formation 贪心二进制

                                                    B - 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 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{AB}).

    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

    题意:给你n个数,让你找出组合A,B,A|B>max(A,B);的个数
    题解: 贪心
    #include<map>
    #include<set>
    #include<cmath>
    #include<queue>
    #include<cstdio>
    #include<vector>
    #include<string>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #pragma comment(linker, "/STACK:102400000,102400000")
    using namespace std;
    const int N=1010;
    const int MAX=151;
    const int MOD=1000000007;
    const int INF=1000000000;
    const double EPS=0.00000001;
    typedef long long ll;
    int read()
    {
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int a[100010],num[35];
    int get(int x)
    {
        int i,ret=0;
        for (i=0;i<31;i++)
        if ((1<<i)>x) break ;
        else if (!((1<<i)&x)) ret+=num[i];
        num[i-1]++;
        return ret;
    }
    int main()
    {
        int i,n,T;
        ll ans;
        scanf("%d", &T);
        while (T--) {
            scanf("%d", &n);
            for (i=1;i<=n;i++) scanf("%d", &a[i]);
            sort(a+1,a+n+1);
            ans=0;
            memset(num,0,sizeof(num));
            for (i=1;i<=n;i++) ans+=get(a[i]);
            printf("%lld
    ", ans);
        }
        return 0;
    }
    代码
  • 相关阅读:
    thinkphp6查询表达式使用between问题
    机器学习纸质作业1
    磁盘挂载
    SQL Server开启READ_COMMITTED_SNAPSHOT
    视觉开发-相机镜头选型
    使用logstash出现报错com.mysql.jdbc.Driver not loaded. Are you sure you've included the correct jdbc driver in :jdbc_driver_library
    linux安装tomcat(转自https://blog.csdn.net/fukai8350/article/details/80467224)
    linux 安装java(转自https://www.cnblogs.com/wjup/p/11041274.html)
    如何统计自动化测试用例的ROI 【投入产出比/投资回报率】
    老男孩老师的博客地址
  • 原文地址:https://www.cnblogs.com/zxhl/p/4865516.html
Copyright © 2011-2022 走看看