HDU 4825 Xor Sum(01字典树)题解

思路:先把所有数字存进字典树,然后从最高位贪心。

代码:

#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
typedef long long ll;
const int maxn = 100000 + 10;
const int seed = 131;
const ll MOD = 1e9 + 7;
const ll INF = 1e17;
using namespace std;
int ch[32 * maxn][2], tot;
ll val[32 * maxn];
void init(){
    tot = 1;
    memset(ch[0], 0, sizeof(ch[0]));
}
void Insert(ll x){
    int root = 0;
    for(int i = 31; i >= 0; i--){
        int u = (x >> i) & 1;
        if(ch[root][u] == 0){
            memset(ch[tot], 0, sizeof(ch[root]));
            ch[root][u] = tot++;
        }
        root = ch[root][u];
    }
    val[root] = x;
}
ll query(ll x){
    int root = 0;
    for(int i = 31; i >= 0; i--){
        int u = (x >> i) & 1;
        if(ch[root][!u] != 0){
            root = ch[root][!u];
        }
        else root = ch[root][u];
    }
    return val[root];
}
int main(){
    int T, Case = 1;
    scanf("%d", &T);
    while(T--){
        init();
        int n, m;
        scanf("%d%d", &n, &m);
        for(int i = 0; i < n; i++){
            ll x;
            scanf("%lld", &x);
            Insert(x);
        }

        printf("Case #%d:\n", Case++);
        while(m--){
            ll x;
            scanf("%lld", &x);
            printf("%lld\n", query(x));
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/KirinSB/p/9827658.html

时间: 2024-07-29 06:17:18

HDU 4825 Xor Sum(01字典树)题解的相关文章

Xor Sum 01字典树 hdu4825

Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total Submission(s): 4119    Accepted Submission(s): 1796 Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Ze

HDU 4825 Xor Sum 字典树+位运算

点击打开链接 Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total Submission(s): 291    Accepted Submission(s): 151 Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus

HDU 4825 Xor Sum(二进制的字典树,数组模拟)

题目 //居然可以用字典树...//用cin,cout等输入输出会超时 //这是从别处复制来的 #include<cstdio> #include<algorithm> #include<cstring> using namespace std; int node[3011111][2]; int tag,m,n,cas=0,T; long long one[64],allone,tmp; //0/1树的加数据操作,增加一个32的数 //其中如果当前位是0,则加左儿子,

hdu 4825 Xor Sum(trie+贪心)

hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #define CLR(a,b) memset((a)

HDU - 4825 Xor Sum(01字典树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题目: Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整数 S ,之后 Zeus 需要在集合当中找出一个正整数 K ,使得 K 与 S 的异或结果最大.Prometheus 为了让 Zeus 看到人类的伟

HDU 4825 Xor Sum(01字典树入门题)

http://acm.hdu.edu.cn/showproblem.php?pid=4825 题意: 给出一些数,然后给出多个询问,每个询问要从之前给出的数中选择异或起来后值最大的数. 思路:将给出的数建立01字典树,从高位开始建树.对于每个询问,如果当前位置值为0,那么在字典树中,如果有1的值,那么就优先走1,否则再走0. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using names

HDU 4825 Xor Sum 【01字典树】

题面: Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整数 S ,之后 Zeus 需要在集合当中找出一个正整数 K ,使得 K 与 S 的异或结果最大.Prometheus 为了让 Zeus 看到人类的伟大,随即同意 Zeus 可以向人类求助.你能证明人类的智慧么? Input 输入包含若干组测试数据,每组

HDU 4825 Xor Sum(经典01字典树+贪心)

Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total Submission(s): 1555    Accepted Submission(s): 657 Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Ze

hdu 4825 xor sum(字典树+位运算)

Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total Submission(s): 4144    Accepted Submission(s): 1810 Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Ze