D. Dr. Evil Underscores(分治)

D. Dr. Evil Underscores
D. Dr. Evil Underscores
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
inputCopy
3
1 2 3
outputCopy
2
inputCopy
2
1 5
outputCopy
4
Note
In the first sample, we can choose X=3.

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

思路:在每一个数都为1或0时可以不用管这一位,如果都有则选择选0/1两者之间最小的就行了:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <cmath>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 1e5+100;
const int maxn2= 6010;
const int inf =0x3f3f3f3f;
int n;
vector<int>q;
int dfs(int n,vector<int> &q){
    vector<int>v1,v2;
    if(n<0) return 0;
    for(int i=0;i<q.size();i++){
        if((q[i]>>n)&1) v2.push_back(q[i]);
        else v1.push_back(q[i]);
    }
    if(v1.size()==0) return dfs(n-1,v2);
    else if(v2.size()==0) return dfs(n-1,v1);
    else return min(dfs(n-1,v1),dfs(n-1,v2))+(1<<n);
}
int main()
{
    cout<<(1^2^3^3)<<endl;
    while(~scanf("%d",&n)){
        for(int i=1;i<=n;i++){
            int a;scanf("%d",&a);
            q.push_back(a);
        }
        printf("%d\n",dfs(30,q));
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值