CF1285 D. Dr. Evil Underscores

Today, as a friendship gift, Bakry gave Badawy n integers a1,a2,…,an and challenged him to choose an integer X such that the value 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).

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

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

Output
Print one integer — the minimum possible value of 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=3.

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

题意
取一个数x,与序列中每个数异或后最大值,使这个数最小,输出

思路
如果某一位都是一样的肯定这一位是0,其他情况都是1,用dfs把序列分成两部分,分别是这一位是0,和是1的,然后再跑,跑完将最小值传递上去。

代码

#include <bits/stdc++.h>
typedef long long ll;
const ll mod = 9999999967;
using namespace std;
namespace fastIO {
    inline void input(ll& res) {
        char c = getchar();res = 0;int f = 1;
        while (!isdigit(c)) { f ^= c == '-'; c = getchar(); }
        while (isdigit(c)) { res = (res << 3) + (res << 1) + (c ^ 48);c = getchar(); }
        res = f ? res : -res;
    }
    inline ll qpow(ll a, ll b) {
        ll ans = 1, base = a;
        while (b) {
            if (b & 1) ans = (ans * base % mod +mod )%mod;
            base = (base * base % mod + mod)%mod;
            b >>= 1;
        }
        return ans;
    }
}
using namespace fastIO;
const int N = 1e6 + 5;
ll Case,n,x;
ll solve(vector<ll> &zr,int n){
	vector<ll> l,r;
	if(n<0) return 0;
	for(auto i:zr){
		if(i&(1<<n)) l.push_back(i);
		else r.push_back(i);
	}
	if(l.size()==0&&r.size()==0) return 0;
	if(l.size()==0) return solve(r,n-1);
	if(r.size()==0) return solve(l,n-1);
	return min(solve(l,n-1),solve(r,n-1))+(1<<n);
}
int main(){
	Case=1;
	vector<ll> zr;
	//input(Case);
	while(Case--){
		input(n);
		for(int i=1;i<=n;i++){
			input(x);
			zr.push_back(x);
		}
		printf("%lld",solve(zr,30));
	}
	return 0;
}
/*
1 0001
5 0101
4 0100
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值