XOR Clique

BaoBao has a sequence a​1​,a​2,...,a​n. He would like to find a subset S of {1,2,...,n} such that ∀i,j∈S, a​i ⊕a​j<min(ai ,aj) and ∣S∣ is maximum, where ⊕ means bitwise exclusive or.

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 (1≤n≤100000), indicating the length of the sequence.

The second line contains n integers: a​1​​ ,a​​2​ ,...,a​n(1≤ai≤10的9次方), indicating the sequence.

It is guaranteed that the sum of n in all cases does not exceed 100000.

Output

For each test case, output an integer denoting the maximum size of S.

Sample Input

3

3

1 2 3

3

1 1 1

5

1 2323 534 534 5

Sample Output

2

3

2

题意,求在a数组中找出s子集,要求子集中任意两个数异或之后比这两个数都小,求最大子集里面元素的个数;

可以知道,2进制的0和1异或才是1,其他是0,要求两个数异或要更小,则必须两个相同长度的数二进制的最高位要都是1,所以,只需要知道在每个长度的集合中,存在多少个元素,元素最多的那个就是答案;

例3中,对应的log2(x)为0,11,9,9,2,可以得到534和534在长度9的子集中,最大子集元素个数为2;

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<string.h>
  4. #include<cmath>
  5. #include<map>
  6. using namespace std;
  7. map<int,int>m;//记录符合条件的子集元素个数
  8. int main()
  9. {
  10. //init();
  11. int T;
  12. scanf("%d",&T);
  13. while(T--)
  14. {
  15. m.clear();
  16. int n,x;
  17. scanf("%d",&n);
  18. while(n--){
  19. scanf("%d",&x);
  20. m[log2(x)]++;//求二进制位数并记录出现次数
  21. }
  22. int cnt=0;
  23. map<int,int>::iterator it;
  24. for(it=m.begin();it!=m.end();it++)
  25. cnt=max(cnt,it->second);//求m中最大值
  26. printf("%d\n",cnt);
  27. }
  28. return 0;
  29. }
  30. /*
  31. log2(x)求法
  32. int w[31];
  33. void init(){
  34. for(int i=0;i<30;i++)
  35. w[i]=1<<i;
  36. }
  37. int find(int x)
  38. {
  39. int l=0,r=30;
  40. while(r-l>1)
  41. {
  42. int mid=l+r>>1;
  43. if(w[mid]<=x)
  44. l=mid;
  45. else r=mid;
  46. }
  47. if(w[r]<x)
  48. return r;
  49. else
  50. return l;
  51. }*/

ZOJ 4057 XOR Clique(位运算)的更多相关文章

  1. ACM学习历程—HDU5269 ZYB loves Xor I(位运算 && dfs && 排序)(BestCoder Round #44 1002题)

    Problem Description Memphis loves xor very musch.Now he gets an array A.The length of A is n.Now he ...

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

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

  3. 位运算 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^ ...

  4. LeetCode - 136. Single Number - ( C++ ) - 解题报告 - 位运算思路 xor

    1.题目大意 Given an array of integers, every element appears twice except for one. Find that single one. ...

  5. delphi 按位运算 not and or xor shl shr

    delphi 按位运算 not and or xor shl shr unit Unit1;   interface   uses   Windows, Messages, SysUtils, Var ...

  6. Xor Sum 2(位运算)

    D - Xor Sum 2 Time limit : 2sec / Memory limit : 1024MB Score : 500 points Problem Statement There i ...

  7. XOR Clique(按位异或)

    XOR Clique(按位异或): 传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4057 准备:异或:参加运算的两 ...

  8. 简简单单学会C#位运算

    一.理解位运算 要学会位运算,首先要清楚什么是位运算?程序中的所有内容在计算机内存中都是以二进制的形式储存的(即:0或1),位运算就是直接对在内存中的二进制数的每位进行运算操作 二.理解数字进制 上面 ...

  9. Java中的位运算

    昨天去面试的时候做到了一道Java的位运算题目,发现有个运算符不懂:">>>",今天特地查了一下,并小结一下常见的位运算符号: ~  按位非(NOT)(一元运算) ...

随机推荐

  1. 前端-----html(1)

    基本结构 Doctype Doctype告诉浏览器使用什么样的html或xhtml规范来解析html文档 <!DOCTYPE html> bead标签 Meta 提供有关页面的元信息,例: ...

  2. 关于Oracle数据库后台进程

    为了最大限度地提高性能并适应许多用户,多进程Oracle数据库系统使用后台进程.后台进程将合并功能,否则这些功能将由运行于每个用户进程的多个数据库程序处理.后台进程异步执行I / O并监视其他Orac ...

  3. 引入第三方SDK allowBackup value不一致引起的编译异常

    项目中要引入一个客服的SDK,项目中 <application android:name=".AppApplication" android:allowBackup=&quo ...

  4. Linux登录失败:Too many logins for 'username'.

    cat /etc/security/limits.conf #<domain> <type> <item> <value> * - maxlogins ...

  5. Keepalived详解(四):通过vrrp_script实现对集群资源的监控【转】

    一.通过vrrp_script实现对集群资源的监控: Keepalived基础HA功能时用到了vrrp_script这个模块,此模块专门用于对集群中服务资源进行监控.与此模块一起使用的还有track_ ...

  6. linux shell中 if else for循环以及大于、小于、等于逻辑表达式的历程

    作者:邓聪聪 比如比较字符串.判断文件是否存在及是否可读等,通常用"[]"来表示条件测试. 注意:这里的空格很重要.要确保方括号的空格.笔者就曾因为空格缺少或位置不对,而浪费好多宝 ...

  7. 2017.12.10《“剑锋OI”普及组多校联盟系列赛(14)#Sooke#Kornal 的课余时间 》分析报告

    报告内容如下 - - [导语] ------ 太晚了,时间也紧,一切尽量从简吧 PS:本文题目来自剑锋OI 所以废话也不多说,进入正题吧,代码直接跟在题目后边儿,主要分析在代码前,次要的就写在代码后面 ...

  8. php password_hash

    2018-1-6 17:22:13 星期六 php 自带的密码生成方法, 如果生成密码最好使用这个, 这个是"单程"的, 没有解密函数 $a = '123456'; $b = pa ...

  9. bootstrap DataTable绑定数据带服务器分页

    <!-- DataTables -->  这两个文件在我的文件夹里面<script src="~/bower_components/datatables.net/js/jq ...

  10. VUE 浏览器关闭时清空localStorage

    1. 概述 1.1 说明 vue项目中,为了解决页面刷新时vuex数据丢失问题,使用localStorage进行存储对应的vuex数据(判断对应localStorage是否为空,不为空则为vuex中数 ...