ZOJ 4057 XOR Clique(位运算)
XOR Clique
BaoBao has a sequence a1,a2,...,an. He would like to find a subset S of {1,2,...,n} such that ∀i,j∈S, ai ⊕aj<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: a1 ,a2 ,...,an(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;
#include<iostream>
#include<algorithm>
#include<string.h>
#include<cmath>
#include<map>
using namespace std;
map<int,int>m;//记录符合条件的子集元素个数
int main()
{
//init();
int T;
scanf("%d",&T);
while(T--)
{
m.clear();
int n,x;
scanf("%d",&n);
while(n--){
scanf("%d",&x);
m[log2(x)]++;//求二进制位数并记录出现次数
}
int cnt=0;
map<int,int>::iterator it;
for(it=m.begin();it!=m.end();it++)
cnt=max(cnt,it->second);//求m中最大值
printf("%d\n",cnt);
}
return 0;
}
/*
log2(x)求法
int w[31];
void init(){
for(int i=0;i<30;i++)
w[i]=1<<i;
}
int find(int x)
{
int l=0,r=30;
while(r-l>1)
{
int mid=l+r>>1;
if(w[mid]<=x)
l=mid;
else r=mid;
}
if(w[r]<x)
return r;
else
return l;
}*/
ZOJ 4057 XOR Clique(位运算)的更多相关文章
- 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 ...
- ZOJ 3870 Team Formation 位运算 位异或用与运算做的
For an upcoming programming contest, Edward, the headmaster of Marjar University, is forming a two-m ...
- 位运算 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^ ...
- LeetCode - 136. Single Number - ( C++ ) - 解题报告 - 位运算思路 xor
1.题目大意 Given an array of integers, every element appears twice except for one. Find that single one. ...
- delphi 按位运算 not and or xor shl shr
delphi 按位运算 not and or xor shl shr unit Unit1; interface uses Windows, Messages, SysUtils, Var ...
- Xor Sum 2(位运算)
D - Xor Sum 2 Time limit : 2sec / Memory limit : 1024MB Score : 500 points Problem Statement There i ...
- XOR Clique(按位异或)
XOR Clique(按位异或): 传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4057 准备:异或:参加运算的两 ...
- 简简单单学会C#位运算
一.理解位运算 要学会位运算,首先要清楚什么是位运算?程序中的所有内容在计算机内存中都是以二进制的形式储存的(即:0或1),位运算就是直接对在内存中的二进制数的每位进行运算操作 二.理解数字进制 上面 ...
- Java中的位运算
昨天去面试的时候做到了一道Java的位运算题目,发现有个运算符不懂:">>>",今天特地查了一下,并小结一下常见的位运算符号: ~ 按位非(NOT)(一元运算) ...
随机推荐
- python正则下载图片
import urllib.request import re # 打开图片路径 def open_url(url): #设置请求路径 req = urllib.request.Request(url ...
- ps遇到的技术问题列表
1.ps矩形选框显示像素 CTRL+K 进入首选项设置就可以了. 2.ps显示辅助线 页面工具栏上的视图按钮,我们在列表上找到标尺,我们也是可以快捷键选择CtrI+R 3.如何将插入photoshop ...
- 【VMware vSphere】再谈VMware vSphere
写在前面 在进行操作vSphere产品之前,就曾经对它进行过一个简单了解:[运维]VMware vSphere简单了解,现在再回头看,发现了解的真的是太简单了.经过前一段时间学习之后,对它又有了新的感 ...
- 统计分析与R软件-chapter2-6
2.6 列表与数据框 2.6.1 列表 1.列表的构造 列表是一种特别的对象集合,它的元素也由序号(下标)区分,但是各元素的类型可以是任意对象,不同元素不必是同一类型,元素本身允许是其他复杂数据类型, ...
- windows下安装MySql + navicat(图形化界面)
MySQL安装过程参考:https://www.cnblogs.com/ayyl/p/5978418.html navicat图形化界面安装过程参考:https://www.cnblogs.com/l ...
- javascript面向对象学习
1.this指向问题,指向的是当前的方法属于谁,当前的方法属于谁就指向谁!! 例子: oDiv.onclick = function () { this指向的是oDiv,因为这个方法属于oDiv } ...
- 【转】Leveldb源码分析——1
先来看看Leveldb的基本框架,几大关键组件,如图1-1所示. Leveldb是一种基于operation log的文件系统,是Log-Structured-Merge Tree的典型实现.LSM源 ...
- 让NotePad++添加到右键快捷方式
添加后的效果图: 操作方式: 第一步:在桌面上新建一个txt文本文档,然后将写入如下内容 Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT ...
- 前端 ----关于DOM的事件操作
关于DOM的事件操作 一.JavaScript的组成 JavaScript基础分为三个部分: ECMAScript:JavaScript的语法标准.包括变量.表达式.运算符.函数.if语句.for ...
- <转载>关系规范化之求最小函数依赖集(最小覆盖)
原文链接http://blog.csdn.net/icurious/article/details/51240114 最小函数依赖集 一.等价和覆盖 定义:关系模式R<U,F>上的两个依赖 ...