当前位置:首页 >> 开发者生态 >> 【Trie】最大异或对(ybtoj Trie-2),博回播

【Trie】最大异或对(ybtoj Trie-2),博回播

cpugpu芯片开发光刻机 开发者生态 5
文件名:【Trie】最大异或对(ybtoj Trie-2),博回播 【Trie】最大异或对(ybtoj Trie-2)

正题 ybtoj Trie-2


题目大意

给你n个数,选择2个,使其异或值最大


解题思路

对于每个数的二进制建立Trie,然后每个数在Trie中搜索,每次尽量走不同方向


代码 #include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define ll long long#define N 100100using namespace std;int n, x, w, ans, to[N*32][2];void insert(int x){int now = 0, y;for (int i = 30; i >= 0; --i){y = (x>>i)&1;if (!to[now][y]) to[now][y] = ++w;now = to[now][y];}return;}int ask(int x){int ans = 0, now = 0, y = 0;for (int i = 30; i >= 0; --i){y = (x>>i)&1^1;//走不同方向if (to[now][y]) ans += 1<<i;else y ^= 1;now = to[now][y];}return ans;}int main(){scanf("%d", &n);for (int i = 1; i <= n; ++i){scanf("%d",&x);ans = max(ans, ask(x));insert(x);}printf("%d", ans);return 0;}
协助本站SEO优化一下,谢谢!
关键词不能为空
同类推荐
«    2026年1月    »
1234
567891011
12131415161718
19202122232425
262728293031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
文章归档
网站收藏
友情链接