【每日一题】The XOR Largest Pair

The XOR Largest Pair:字典树

https://ac.nowcoder.com/acm/problem/50993

经典字典树求n个数中任意两个数的异或值最大。

思路:利用32位二进制位构建字典树。然后我们知道异或值是相同位0,不同为1,所以对于每个数取遍历字典树,如果这个数的某位取反在字典树上是存在的话,则进入这个分支,否则则按原路走。更新得到最大值即可。

k = (x>>i)&1;注意不能写成k=((1<<i)&x),因为我们要取得是1/0,不是1000.../00000

​
#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;
const int N = 2e6+7;
const ll ds = 1e15+7;
const double PI = 3.141592653589793238462643383;
 
using namespace std;

int trie[N][5];
ll a[N];
int pos = 1;
void Insert(ll x)
{
	int p = 0;
	for(int i = 31; i >= 0; i--){
		int k = (x>>i)&1;
		if(trie[p][k] == 0) trie[p][k] = pos++;
		p = trie[p][k];
	}
}

ll find(ll x)
{
	int p = 0;
	ll ans = 0;
	for(int i = 31; i >= 0; i--){
		int k = (x>>i)&1;//注意不能写成k=((1<<i)&x),因为我们要取得是1/0,不是1000.../00000
		int kk = !(k);
		if(trie[p][kk] == 0){
			p = trie[p][k];
			ans = (ans<<1);
		}
		else{
			p = trie[p][kk];
			ans = (ans<<1)+1;
		}
	}
	return ans;
}

void solve() {
	int n;
	cin >> n;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		Insert(a[i]);
	}
	ll ans = -1;
	for(int i = 1; i <= n; i++){
		ans = max(ans,find(a[i]));
	}
	cout << ans << endl;
}

int main() { 
//	int t;
//	scanf("%d",&t);
//	while(t--)
		solve();
	return 0;
}
​

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值