Dr. Evil Underscores(异或最大值最小)

Today, as a friendship gift, Bakry gave Badawy nn integers a1,a2,…,ana1,a2,…,an and challenged him to choose an integer XX such that the value max1≤i≤n(ai⊕X)max1≤i≤n(ai⊕X) is minimum possible, where ⊕⊕ denotes the bitwise XOR operation.

As always, Badawy is too lazy, so you decided to help him and find the minimum possible value of max1≤i≤n(ai⊕X)max1≤i≤n(ai⊕X).

Input
The first line contains integer nn (1≤n≤1051≤n≤105).

The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤230−10≤ai≤230−1).

Output
Print one integer — the minimum possible value of max1≤i≤n(ai⊕X)max1≤i≤n(ai⊕X).

Examples
Input
3
1 2 3
Output
2
Input
2
1 5
Output
4
Note
In the first sample, we can choose X=3X=3.

In the second sample, we can choose X=5X=5.

题意:找出一个数X使得在这里插入图片描述最小,并求出这个值。
思路:异或值最大,首先想到的是字典树,但是根据这个思路,如果说这一位异或之后的数字相同,那么这一位就不产生贡献。如果说这一位异或之后的数字不相同,那么就产生贡献(1<<k)。但是我们要求最大值最小,我们只需要看后面的数产生的贡献哪个比较小就可以了。这样我们把这一位是1的,和这一位是0的分成两个集合,分别递归下去,取返回值较小的,再加上(1<<k)就可以了。
代码如下:

#include<bits/stdc++.h>
using namespace std;

const int maxx=1e5+100;
int n;

inline int dfs(int k,vector<int> &p)
{
	if(k<0) return 0;
	if(p.size()==0) return 0;
	vector<int> p1,p0;
	for(int i=0;i<p.size();i++)
	{
		if((p[i]>>k)&1) p1.push_back(p[i]);
		else p0.push_back(p[i]);
	}
	if(p0.size()==0) return dfs(k-1,p1);
	if(p1.size()==0) return dfs(k-1,p0);
	return min(dfs(k-1,p1),dfs(k-1,p0))+(1<<k);
}
int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		vector<int> p;
		int x;
		for(int i=1;i<=n;i++) 
		{
			scanf("%d",&x);
			p.push_back(x);
		}
		cout<<dfs(30,p)<<endl;
	}
	return 0;
}

努力加油a啊,(o)/~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值