Team Formation

 1 #include <stdio.h>
 2 #include <ctype.h>
 3 #include <math.h>
 4 #include <stack>
 5 #include <queue>
 6 #include <stdlib.h>
 7 #include <string.h>
 8 #include <iostream>
 9 #include <algorithm>
10 using namespace std;
11 const int MM=100002;
12 int num[MM];
13 int bit[32];//记录下小的数的最高位的位置;
14 int b[32];//记录当前数的二进制 顺序倒着
15 void change(int n)
16 {
17    int i=0;
18    while(n)
19    {
20       b[i++]=n%2;
21       n=n/2;
22    }
23 }
24 int main()
25 {
26    int t,n,cnt;
27    cin>>t;
28    while(t--)
29    {
30       cin>>n;
31       memset(bit,0,sizeof bit);
32       cnt=0;
33       for(int i=0;i<n;i++)
34       {
35          cin>>num[i];
36       }
37       sort(num,num+n);
38       int tmp=(int)log2(num[0]);//最高位1的位置
39       bit[tmp]++;
40       for(int i=1;i<n;i++)
41       {
42          int temp=(int)log2(num[i]);
43          memset(b,0,sizeof b);
44          change(num[i]);//十进制转二进制
45          for(int j=0;j<temp;j++)
46          {
47             
48             if(b[j]==0)
49                cnt+=bit[j];
50          }
51          bit[temp]++;//更新当前的bit
52       }
53       printf("%d
",cnt);
54    }
55    return 0;
56 }
原文地址:https://www.cnblogs.com/sylvialucy/p/4457694.html