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

解题思路:第二组样例化为二进制分别为 1  10  11  100  101。可以发现,如果要让一个数增大,只要该数化为二进制后的出现0的位置跟1异或就会变大,同时需要满足另一个数的最高位为该数出现0位置的位数,如10可以跟1异或变为11 ,100可以跟10、11、1异或分别变为110,111,101,而101只能跟两位的进行异或,因为它的0出现的位置为第二位,最后求和就行了。

#include<bits/stdc++.h>
using namespace std;
int a[101000];
void solve(int n) {
int num[50],num_1[50];
//num_1[i]表示某数化为二进制最高位为i的个数
//num[i]记录某数化为二进制后最高位为i且与前边出现数字异或值满足条件的组合的个数
sort(a,a+n);//将所给数组由小到大排序
memset(num,0,sizeof(num));
memset(num_1,0,sizeof(num_1));
int tm=a[0];
int pos_1=0,pos_0=0; //pos_0记录化为二进制过程中0出现的位置
//pos_1记录化为二进制过程中1出现的位置
while(tm!=0){ //处理数组a中最小的值
if(tm%2==0){
pos_0=pos_1+1;
/* 由于这是a数组的第一个数且num1、num数组初始值都为0,所以这里没有求
这个数化二进制过程中出现0的位置在前边的数化二进制过程中为最高位的位
置的个数
*/
}
tm>>=1;
pos_1++;
}
num_1[pos_1]++; //最高位为pos_1位置的数组变量自加
for(int i=1;i<n;i++) {
pos_1=0,pos_0=0;
tm=a[i];
int tmp=0;
while(tm!=0){
if(tm%2==0){
pos_0=pos_1+1;
tmp+=num_1[pos_0];
//tmp累加前边二进制最高位为pos_0位置的个数
}
tm>>=1;
pos_1++;
}
num_1[pos_1]++;
num[pos_1]+=tmp; //这个数可以跟前边的数异或值增大的个数
}
int ans=0;
for(int i=0;i<50;i++) {
ans+=num[i];
}
printf("%d\n",ans);
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
solve(n);
}
return 0;
} /* 5
5
1 2 3 4 5 */

  

Zoj 3870——Team Formation——————【技巧,规律】的更多相关文章

  1. 位运算 ZOJ 3870 Team Formation

    题目传送门 /* 题意:找出符合 A^B > max (A, B) 的组数: 位运算:异或的性质,1^1=0, 1^0=1, 0^1=1, 0^0=0:与的性质:1^1=1, 1^0=0, 0^ ...

  2. ZOJ 3870 Team Formation 贪心二进制

                                                    B - Team Formation Description For an upcoming progr ...

  3. ZOJ 3870 Team Formation 位运算 位异或用与运算做的

    For an upcoming programming contest, Edward, the headmaster of Marjar University, is forming a two-m ...

  4. ZOJ - 3870 Team Formation(异或)

    题意:给定N个数,求这N个数中满足A ⊕ B > max{A, B})的AB有多少对.(A,B是N中的某两个数) 分析: 1.异或,首先想到转化为二进制. eg:110011(A)和 1(B)- ...

  5. 2015 浙江省赛B Team Formation (技巧,动归)

    Team Formation For an upcoming programming contest, Edward, the headmaster of Marjar University, is ...

  6. 费用流 ZOJ 3933 Team Formation

    题目链接 题意:两个队伍,有一些边相连,问最大组对数以及最多女生数量 分析:费用流模板题,设置两个超级源点和汇点,边的容量为1,费用为男生数量.建边不能重复建边否则会T.zkw费用流在稠密图跑得快,普 ...

  7. ZOJ 3933 Team Formation

    费用流裸题......比赛的时候少写了一句话....导致增加了很多无用的边一直在TLE #include<cstdio> #include<cstring> #include& ...

  8. ZOJ 3870:Team Formation(位运算&思维)

    Team Formation Time Limit: 2 Seconds Memory Limit: 131072 KB For an upcoming programming contest, Ed ...

  9. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Team Formation

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5494 The 12th Zhejiang Provincial ...

随机推荐

  1. 以太坊系列之十三: evm指令集

    evm指令集手册 Opcodes 结果列为"-"表示没有运算结果(不会在栈上产生值),为"*"是特殊情况,其他都表示运算产生唯一值,并放在栈顶. mem[a.. ...

  2. javascript webstorm用法

    javascript  webstorm用法 一.什么是webstorm?       WebStorm 是jetbrains公司旗下一款JavaScript 开发工具.被广大中国JS开发者誉为“We ...

  3. python3好用的mysql.connector库

    python3好用的mysql.connector库 from mysql.connector import connect #建立mysql连接,生成一个mysql.connector对象 conn ...

  4. 【转】ASCII码十进制、十六进制对照表

    源地址:https://www.baidu.com/link?url=3bScEOF5BVXt7ptGDjBV5JmynXHqEh5lyut1vUx6vEM7qqPY1vtbO22Vvy7xUoFd& ...

  5. 配置Java web的一次经历

    最近在完成数据库作业,重新拾起了以前学过的Java,讲下自己的 Java web 配置过程. 1.安装 Tomcat 在官网下载 Tomcat7.0版本:https://tomcat.apache.o ...

  6. 基于Spring MVC的文件上传和下载功能的实现

    配置文件中配置扫描包,以便创建各个类的bean对象 <context:component-scan base-package="com.neuedu.spring_mvc"& ...

  7. 中国版Azure 文件服务

    中国版Azure 文件服务预览版在2015年1月13日已经上线,文件存储使用标准SMB 2.1协议为应用程序提供共享存储. 当我们的虚拟机连接到文件共享后就可以像使用本地共享文件夹一样来读取和写入文件 ...

  8. 选择排序 思想 JAVA实现

    已知一个数组 9.29.95.47.79.37.18.56.96.22 使用选择排序是数组有序 选择排序同样是一个运行时间为O(N²)的排序算法. 算法思想:(以从小到大为例) 9.29.95.47. ...

  9. eazyui 或bootstrap table表格里插入图片,点击查看大图

    columns: [ {field: 'state',checkbox: true,align: 'center',valign: 'middle'}, {field: 'contacts',sort ...

  10. Linux磁盘占满 no space left on device

    假如当前文件删除了,如果还有其他进程还在使用这个文件,这个文件删不干净:https://www.cnblogs.com/heyonggang/p/3644736.html 在Linux下查看磁盘空间使 ...