Xor Sum HDU
来源:互联网 发布:淘宝注册账号申请在线 编辑:程序博客网 时间:2024/06/05 19:17
题意:n个数字,m个询问,问n个数字中与给定的x异或最大的数字是多少。
思路:从高位到低位插入01值,因为异或时高位尽量大,结果才尽量大,最低位节点保存相应的值,最后查询时,尽量找能使该位变成1的节点遍历。
#include <iostream> #include <cstdio> #include <bitset> #include <cstring> #include <algorithm> #include <vector> #include <map> #include <cmath> #include <set> #include <queue> using namespace std; typedef long long ll; const int INF=1e9+100; const int mod=1e9+7;const int N=100005; ll ch[32*N][2],sz,val[N*32];int n,m;void init(){ sz=1; memset(ch[0],0,sizeof(ch[0]));}void insert(ll a){ int u=0; for(int i=32;i>=0;i--){ int c=(a>>i)&1; if(!ch[u][c]){ memset(ch[sz],0,sizeof(ch[sz])); //val[sz]=0; ch[u][c]=sz++; } u=ch[u][c]; } val[u]=a;}ll query(ll a){ int u=0; for(int i=32;i>=0;i--){ int c=(a>>i)&1; if(ch[u][c^1]) u=ch[u][c^1]; else u=ch[u][c]; } return val[u];}int main(){ //freopen("out.txt","w",stdout); int T,cas=1; ll a; scanf("%d",&T); while(T--){ init(); scanf("%d %d",&n,&m); for(int i=0;i<n;i++){ scanf("%lld",&a); insert(a); } printf("Case #%d:\n",cas++ ); while(m--){ scanf("%lld",&a); printf("%lld\n",query(a) ); } } return 0;}
阅读全文
0 0
- HDU 4825 Xor Sum
- hdu 4825 Xor Sum
- HDU 4825 Xor Sum
- HDU-4825 Xor Sum
- HDU 4825 Xor Sum
- Xor Sum HDU
- Xor Sum HDU
- Xor Sum HDU
- Xor Sum HDU
- HDU 4825-Xor Sum
- hdu 4825 Xor Sum(字典树)
- 【01字典树】HDU 4825 Xor Sum
- hdu 4825 Xor Sum(字典树)
- HDU 4825 Xor Sum(trie树+贪心)
- HDU-4825-Xor Sum【字典树】
- HDU 4825 Xor Sum 01字典树
- HDU 4825 Xor Sum (01字典树)
- HDU-Xor Sum(01字典树)
- NYOJ 469 擅长排列的小明 II
- Android 控件边框
- 递归和非递归遍历二叉树
- 计算二进制形式中1的个数的经典算法
- React-native基础介绍
- Xor Sum HDU
- iOS方法--------单例模式的写法
- 同步更新测试
- POJ 2524-Ubiquitous Religions
- HttpClient使用SESSIONID模拟用户发送请求
- 常用排序算法的js实现(冒泡、选择、插入、归并、希尔、快排)
- SVN二次开发收集01
- 工厂方法模式
- SSM超强大整合,转载。。。