K 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,所以,只需要知道在每个长度的二进制区间中,存在多少个数字,数字最多的那个就是答案

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h> 
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#define sf scanf
#define pf printf
#define scf(x) scanf("%d",&x)
#define scff(x,y) scanf("%d%d",&x,&y)
#define scfff(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define prf(x) printf("%d\n",x) 
#define mm(x,b) memset((x),(b),sizeof(x))
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
const ll mod=1e9+7;
const double eps=1e-8;
const int inf=0x3f3f3f3f;
using namespace std;
const double pi=acos(-1.0);
const int N=1e5+10;
int a[N];
int upp[32];//区间上限
int doww[32];//区间下限
void init()
{
    int x=2;
    upp[0]=1;
    doww[0]=1;
    rep(i,1,32)
    {
        doww[i]=x;
        upp[i-1]=x-1;
        x*=2;
    }
    upp[31]=x-1;
}
int num[32];//存每个区间的数量
int main()
{
    int re;scf(re);
    init();
    while(re--)
    {
        mm(num,0);
        int n;scf(n);
        rep(i,0,n)
        {
            scf(a[i]);
            rep(j,0,32)
            {
                if(a[i]>=doww[j]&&a[i]<=upp[j])
                {
                    num[j]++;
                    break;
                }
            }
        }
        int ans=0;
        rep(i,0,32)
        ans=max(ans,num[i]);
        prf(ans);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/wzl19981116/p/9657116.html
xor
今日推荐