XOR Clique
Time Limit: 1 Second Memory Limit: 65536 KB
BaoBao has a sequence . He would like to find a subset of such that , and is maximum, where means bitwise exclusive or.
Input
There are multiple test cases. The first line of input contains an integer , indicating the number of test cases. For each test case:
The first line contains an integer (), indicating the length of the sequence.
The second line contains n integers: (), indicating the sequence.
It is guaranteed that the sum of in all cases does not exceed .
Output
For each test case, output an integer denoting the maximum size of .
Sample Input
3
3
1 2 3
3
1 1 1
5
1 2323 534 534 5
Sample Output
2
3
题解:给定 n 个整数,求一个集合,是的集合中的任意两个数的异或值小于这两个数。很容易发现当且仅当两个数字最高位二进制相同的时候才满足条件,即为相同二进制最高位的数字数量。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int a[33];
int main() {
int t, n;
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
int x;
memset(a, 0, sizeof a);
for(int i = 0; i < n; i++) {
scanf("%d", &x);
int cnt = 0;
while(x > 0) {
x/=2;
cnt++;
}
a[cnt]++;
}
sort(a,a+33);
reverse(a,a+33);
printf("%d\n", a[0]);
}
return 0;
}