zoukankan      html  css  js  c++  java
  • zoj 3870 Team Formation

    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{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
    要AxorB>max(A,B),则A,B的最大bit不能有相同出现,预处理统计出所有最大bit,再扫一遍统计出总数
     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<cmath>
     4 #include<cstring>
     5 #define e 0.0000001
     6 
     7 int N,n, a[100000][2], bit[70];
     8 bool maxbit[32];
     9 int bitnum[32];
    10 int T,i,j;
    11 
    12 int main()
    13 {
    14     for(int i=0;i<32;i++)
    15         bitnum[i]=1<<i;
    16     scanf("%d", &T);
    17     while (T--)
    18     {
    19         long long count = 0;
    20         memset(bit, 0, sizeof(bit));
    21         memset(maxbit, false, sizeof(maxbit));
    22         scanf("%d", &N);
    23         for (i = 0; i < N; i++)
    24         {
    25             scanf("%d", &a[i][0]);
    26             a[i][1]=30;
    27             while(bitnum[a[i][1]]>a[i][0])
    28                 a[i][1]--;
    29             //a[i][1] = int(log2(a[i][0])+e);
    30             maxbit[a[i][1]]=true;
    31         }
    32         for (i = 0; i < 31; i++)
    33         {
    34             if (maxbit[i])
    35             for (j = 0; j < N; j++)
    36                     if (a[j][0]&bitnum[i]) bit[i]++;
    37         }
    38         n = N-1;
    39         for (i = 0; i < N; i++,n--)
    40         {
    41             count += n-bit[a[i][1]]+1;
    42             bit[a[i][1]]--;
    43         }
    44         printf("%lld
    ", count);
    45     }
    46 }
    View Code
  • 相关阅读:
    静态邻接表dijkstra
    最短路径系列【最短路径、哈密顿路等】
    python 给多人发送邮件,且将结果添加为附件
    Excel调换数据位置
    try ... except...,好处是执行失败后,仍然可以继续运行
    制作表头,2种方式
    工资表变工资条,2种方式
    C言语教程第一章: C言语概论 (4)
    从红旗5.0说起——看Linux的内存解决
    红旗Linux桌面4.1文本安装历程图解(二)
  • 原文地址:https://www.cnblogs.com/cdyboke/p/4875748.html
Copyright © 2011-2022 走看看