传送门
Xor Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 8723 Accepted Submission(s): 3647
Problem Description
Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整数 S ,之后 Zeus 需要在集合当中找出一个正整数 K ,使得 K 与 S 的异或结果最大。Prometheus 为了让 Zeus 看到人类的伟大,随即同意 Zeus 可以向人类求助。你能证明人类的智慧么?
Input
输入包含若干组测试数据,每组测试数据包含若干行。
输入的第一行是一个整数T(T < 10),表示共有T组数据。
每组数据的第一行输入两个正整数N,M(<1=N,M<=100000),接下来一行,包含N个正整数,代表 Zeus 的获得的集合,之后M行,每行一个正整数S,代表 Prometheus 询问的正整数。所有正整数均不超过2^32。
输入的第一行是一个整数T(T < 10),表示共有T组数据。
每组数据的第一行输入两个正整数N,M(<1=N,M<=100000),接下来一行,包含N个正整数,代表 Zeus 的获得的集合,之后M行,每行一个正整数S,代表 Prometheus 询问的正整数。所有正整数均不超过2^32。
Output
对于每组数据,首先需要输出单独一行”Case #?:”,其中问号处应填入当前的数据组数,组数从1开始计算。
对于每个询问,输出一个正整数K,使得K与S异或值最大。
对于每个询问,输出一个正整数K,使得K与S异或值最大。
Sample Input
2
3 2
3 4 5
1
5
4 1
4 6 5 6
3
Sample Output
Case #1: 4 3
Case #2: 4
Source
Recommend
liuyiding
题目大意是输入n个数字作为整个数组
然后有m组查询,每次查询输入一个数字
你需要在log的复杂度时间内查询到与每一个待查询数字异或值最大的那个数组里面的数
字典树处理异或问题的模板题
首先需要明确为什么字典树可以解决查询最大异或值的问题
由于异或操作的特殊性(当且仅当数位不同的时候异或和为1)
那么我们只需要用n个数字构建一棵字典树
查询的时候只需要从高位到低位优先查找和这个数字这个数位不一样的即可
(因为优先高位的话保证了异或结果最大)
最后查询过程中记录结果输出即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
/************************************************************************* >>> Author: WindCry1 >>> Mail: lanceyu120@gmail.com >>> Website: https://windcry1.com >>> Date: 12/21/2019 5:03:27 PM *************************************************************************/ #include <cstring> #include <cmath> #include <cstdio> #include <cctype> #include <cstdlib> #include <ctime> #include <vector> #include <iostream> #include <string> #include <queue> #include <set> #include <map> #include <algorithm> #include <complex> #include <stack> #include <bitset> #include <iomanip> #include <list> #include <sstream> #include <fstream> #if __cplusplus >= 201103L #include <unordered_map> #include <unordered_set> #endif #define ll long long #define ull unsigned long long #define DEBUG(x) cout<<#x<<" : "<<x<<endl; #define lowbit(x) x&(-x) #define ls u<<1 #define rs u<<1|1 using namespace std; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const double clf = 1e-8; const int MMAX = 0x7fffffff; const int INF = 0x3f3f3f3f; const int mod = 1e9+7; const int dir[4][2]={-1,0,1,0,0,-1,0,1}; ostream& operator <<(ostream &out, pii &p){ return out<<p.first<<" "<<p.second; } istream& operator >>(istream &in, pii &p){ return in>>p.first>>p.second; } struct Trie{ int next[2]; }node[100010<<5]; int tot; void insert(ll x){ int now=0; for(int i=32;~i;i--){ bool temp=x&(1LL<<i); if(!node[now].next[temp]) node[now].next[temp]=++tot; now=node[now].next[temp]; } } ll query(ll x){ ll v=0;int now=0; for(int i=32;~i;i--){ bool temp=x&(1LL<<i); if(node[now].next[!temp]) temp=!temp; if(temp) v+=1LL<<i; now=node[now].next[temp]; } return v; } int main(){ ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); //ifstream cin("C:\\Users\\LENOVO\\Desktop\\in.txt"); //ofstream cout("C:\\Users\\LENOVO\\Desktop\\out.txt"); int T;cin>>T;for(int cas=1;cas<=T;cas++){ memset(node,0,sizeof node); tot=0; int n,m;ll t;cin>>n>>m; for(int i=1;i<=n;i++){ cin>>t; insert(t); } cout<<"Case #"<<cas<<":"<<endl; while(m--){ cin>>t; cout<<query(t)<<endl; } } return 0; } |
2019-12-26 14:08 Author: WindCry1
0 条评论