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;
}
代码
原文地址:https://www.cnblogs.com/zxhl/p/4865516.html