The XOR Largest Pair

思路

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1e5 + 10;
int tr[maxn * 32][2], cnt;
int n;
int val[maxn];
void add(int x){
    int cur = 0;
    for(int i = 30; i >= 0; i--){
        int f = (x >> i) & 1;
        if(!tr[cur][f]) tr[cur][f] = ++cnt;
        cur = tr[cur][f];
    }
}
int query(int x){
    int cur = 0, ans = 0;
    for(int i = 30; i >= 0; i--){
        int f = (x >> i) & 1; f ^= 1;
        if(!tr[cur][f]) f ^= 1;
        else ans += 1 << i;
        cur = tr[cur][f];
    }
    return ans;
}
int main(){
    scanf("%d", &n);
    for(int i = 1; i <= n; i++){
        scanf("%d", &val[i]);
        add(val[i]);
    }
    int ans = 0;
    for(int i = 1; i <= n; i++){
        ans = max(ans, query(val[i]));
    }
    printf("%d\n", ans);
    return 0;
}

全部评论

相关推荐

10-15 20:01
已编辑
上海大学 Java
钉钉什么垃圾公司,约面鸽人
Syca_:途虎养车给我定了我这边早上六点的笔试,睡了四个小时起来难受的要命,告诉我面试时间是两天后的凌晨四点
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务