three arrays【2019 HDU多校5】【字典树Trie树+贪心】

HDU-6625 题目链接


  很简单的思路,直接就是去开两个字典树,然后两个字典树一块跑就可以了,每次都是贪心的选取最小异或值即可。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MAX_3(x, y, z) max(x, max(y, z))
#define MAX_6(a, b, c, d, f, g) max(max(a, b), max(max(c, d), max(f, g)))
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 1e5 + 7;
int N, trie[2][maxN*31][2], cnt[2][maxN*31], tot[2];
inline void clear(int op, int rt)
{
    for(int i=0; i<=1; i++) trie[op][rt][i] = 0;
    cnt[op][rt] = 0;
}
inline void Insert(int op, int x)
{
    int root = 0, bit = 0;
    for(int i=30; i>=0; i--)
    {
        bit = (x >> i) & 1;
        if(!trie[op][root][bit])
        {
            trie[op][root][bit] = ++tot[op];
            clear(op, tot[op]);
        }
        root = trie[op][root][bit];
        cnt[op][root]++;
    }
}
inline void Delete(int op, int x)
{
    int root = 0, bit = 0;
    for(int i=30; i>=0; i--)
    {
        bit = (x >> i) & 1;
        if(!( --cnt[op][trie[op][root][bit]] ))
        {
            trie[op][root][bit] = 0;
            break;
        }
        root = trie[op][root][bit];
    }
}
int ans[maxN];
inline void find_first(int &x0, int &x1)
{
    int rt0 = 0, rt1 = 0;
    x0 = 0; x1 = 0;
    for(int i=30; i>=0; i--)
    {
        if(trie[0][rt0][0] && trie[1][rt1][0])
        {
            rt0 = trie[0][rt0][0];
            rt1 = trie[1][rt1][0];
        }
        else if(trie[0][rt0][1] && trie[1][rt1][1])
        {
            rt0 = trie[0][rt0][1];
            rt1 = trie[1][rt1][1];
            x0 |= 1 << i;
            x1 |= 1 << i;
        }
        else
        {
            if(trie[0][rt0][0])
            {
                rt0 = trie[0][rt0][0];
                rt1 = trie[1][rt1][1];
                x1 |= 1 << i;
            }
            else
            {
                rt0 = trie[0][rt0][1];
                rt1 = trie[1][rt1][0];
                x0 |= 1 << i;
            }
        }
    }
}
inline void init()
{
    tot[0] = tot[1] = 0;
    for(int i=0; i<=1; i++)
    {
        cnt[i][0] = 0;
        for(int j=0; j<=1; j++) trie[i][0][j] = 0;
    }
}
int main()
{
    int Cas; scanf("%d", &Cas);
    while(Cas--)
    {
        scanf("%d", &N);
        init();
        for(int i=1, val; i<=N; i++)
        {
            scanf("%d", &val);
            Insert(0, val);
        }
        for(int i=1, val; i<=N; i++)
        {
            scanf("%d", &val);
            Insert(1, val);
        }
        for(int i=1, x0, x1; i<=N; i++)
        {
            find_first(x0, x1);
            ans[i] = x0 ^ x1;
            Delete(0, x0); Delete(1, x1);
        }
        sort(ans + 1, ans + N + 1);
        for(int i=1; i<=N; i++) printf("%d%c", ans[i], i == N ? '\n' : ' ');
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值