ZOJ-3870-Team Formation【位运算】【12th浙江省赛】

ZOJ-3870-Team Formation

                    Time Limit: 3 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. 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

题目链接:ZOJ-3870

题目大意:给出一串数字,每次选两个数字A和B,问有多少种选择方式使得 A ⊕ B > max{A, B}

题目思路:

异或的性质:0 ^ 1 = 1, 1 ^ 0 = 1, 0 ^ 0 = 0,1 ^ 1 = 0,

  1. max_dig[]保存所有数字二进制最高位1所在的位置

  2. other_dig[]保存所有数字除去最高位后,其余数字所在位置

先确定一个A,最高位1在第i位.

确定B需要两个条件:

1. 数字B的最高位‘1’的位置大于i

2. 并且第i位为0

所以数字A满足情况的数字B可能情况数量为, 30j=i+1(max _ dig[j])other _ dig[i]

以下是代码:

#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
int max_dig[100050];
int other_dig[100050];
int num[100050];
int main(){
    int t;
    cin >> t;
    while(t--)
    {
        memset(max_dig,0,sizeof(max_dig));
        memset(other_dig,0,sizeof(other_dig));
        int n;
        scanf("%d",&n);
        for (int i = 0; i < n; i++)
        {
            scanf("%d",&num[i]);
        }
        for (int i = 0; i < n; i++)
        {
            int flag = 1;
            for (int j = 30; j >= 0; j--)
            {
                if ((num[i] >> j) & 1 && flag)
                {
                    max_dig[j]++;  //记录最高位1所在位置
                    flag = 0;
                }
                else if ((num[i] >> j) & 1)
                {
                    other_dig[j]++; //记录除去最高位1的其余1所在位置
                }
            }
        }
        int ans = 0;
        for (int i = 0; i < n; i++)
        {
            for (int j = 30; j >= 0; j--)
            {
                if ((num[i] >> j) & 1)  //找到num[i]的第一个1所在位置
                {
                    for (int k = 30; k > j; k--) //数字B的最高位‘1’的位置大于i
                    {
                        ans += max_dig[k];
                    }
                    ans -= other_dig[j];  //减去第i位为1的情况
                    break;
                }
            }       
        }
        cout << ans << endl; 
    } 
    return 0;
}

你可能感兴趣的:(位运算,ZOJ-3870)