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

  1. 2
  2. 3
  3. 1 2 3
  4. 5
  5. 1 2 3 4 5

Sample Output

  1. 1
  2. 6

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

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int a[101000];
  4. void solve(int n) {
  5. int num[50],num_1[50];
  6. //num_1[i]表示某数化为二进制最高位为i的个数
  7. //num[i]记录某数化为二进制后最高位为i且与前边出现数字异或值满足条件的组合的个数
  8. sort(a,a+n);//将所给数组由小到大排序
  9. memset(num,0,sizeof(num));
  10. memset(num_1,0,sizeof(num_1));
  11. int tm=a[0];
  12. int pos_1=0,pos_0=0; //pos_0记录化为二进制过程中0出现的位置
  13. //pos_1记录化为二进制过程中1出现的位置
  14. while(tm!=0){ //处理数组a中最小的值
  15. if(tm%2==0){
  16. pos_0=pos_1+1;
  17. /* 由于这是a数组的第一个数且num1、num数组初始值都为0,所以这里没有求
  18. 这个数化二进制过程中出现0的位置在前边的数化二进制过程中为最高位的位
  19. 置的个数
  20. */
  21. }
  22. tm>>=1;
  23. pos_1++;
  24. }
  25. num_1[pos_1]++; //最高位为pos_1位置的数组变量自加
  26. for(int i=1;i<n;i++) {
  27. pos_1=0,pos_0=0;
  28. tm=a[i];
  29. int tmp=0;
  30. while(tm!=0){
  31. if(tm%2==0){
  32. pos_0=pos_1+1;
  33. tmp+=num_1[pos_0];
  34. //tmp累加前边二进制最高位为pos_0位置的个数
  35. }
  36. tm>>=1;
  37. pos_1++;
  38. }
  39. num_1[pos_1]++;
  40. num[pos_1]+=tmp; //这个数可以跟前边的数异或值增大的个数
  41. }
  42. int ans=0;
  43. for(int i=0;i<50;i++) {
  44. ans+=num[i];
  45. }
  46. printf("%d\n",ans);
  47. }
  48. int main(){
  49. int t;
  50. scanf("%d",&t);
  51. while(t--){
  52. int n;
  53. scanf("%d",&n);
  54. for(int i=0;i<n;i++)
  55. scanf("%d",&a[i]);
  56. solve(n);
  57. }
  58. return 0;
  59. }
  60.  
  61. /*
  62.  
  63. 5
  64. 5
  65. 1 2 3 4 5
  66.  
  67. */

  

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. 【SQL】- 基础知识梳理(八) - 事务与锁

    事务的概念 事务:若干条T-SQL指令组成的一个操作数据库的最小执行单元,这个整体要么全部成功,要么全部失败.(并发控制) 事务的四个属性:原子性.一致性.隔离性.持久性.称为事务的ACID特性. 原 ...

  2. LOJ#10065. 「一本通 3.1 例 2」北极通讯网络

    题目链接:https://loj.ac/problem/10065 题目描述 原题来自:Waterloo University 2002 北极的某区域共有 nnn 座村庄,每座村庄的坐标用一对整数 ( ...

  3. 1、认识Socket

    专业术语定义:(不易理解浏览大体意思即可) 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket. 建立网络通信连接至少要一对端口号(socket).socket本 ...

  4. CENTOS7 使用 Nginx + Uwsgi 部署 Django 项目

    写在前面的话 最近总是见到有新学 Django 的朋友在部署自己的项目到 Linux 上面的时候运行不起来,所以就动手写了这篇博客. 对于不会搭建 Python 3 环境的朋友可以参考前面的博客[CE ...

  5. 1233: 传球游戏 [DP]

    1233: 传球游戏 [DP] 时间限制: 1 Sec 内存限制: 128 MB 提交: 4 解决: 3 统计 题目描述 上体育课的时候,小蛮的老师经常带着同学们一起做游戏.这次,老师带着同学们一起做 ...

  6. 单据头->实体服务规则中根据单据类型设置可见性或必录等

  7. WPF 仿IPhone滑块开关 样式 - CheckBox

    原文:WPF 仿IPhone滑块开关 样式 - CheckBox <Style x:Key="CheckRadioFocusVisual"> <Setter Pr ...

  8. Macaca,Maven,MVC框架

    Macaca:Macaca是阿里开源的一套完整的自动化测试解决方案.同时支持PC和移动端测试,支持的语言有JS,Java,Python. Maven:java,Maven项目对象模型(POM),可以通 ...

  9. ggplot你不知道的细节

    例一 Michaelis-Menten动力学方程 这个例子中采用出自文献中的一组有关于浮萍氮摄取的数据,共2两个变量8个观测值,其中底物浓度与浮萍的氮取速率之间可以通过M-M动力学方程来进行描述.在这 ...

  10. LeetCode231.2的幂

    231.2的幂 描述 给定一个整数,编写一个函数来判断它是否是 2 的幂次方. 示例 示例 1: 输入: 1 输出: true 解释: 2^0 = 1 示例 2: 输入: 16 输出: true 解释 ...