19 hdu多校 three arrays // 01 Trie

19 hdu多校 three arrays

题意

给数组 a [ ] , b [ ] a[ ],b[ ] a[],b[],长度为 n n n,可以对 a [ ] a[] a[] b [ ] b[] b[]rearrange,使得构造的 c [ ] ( c [ i ] = a [ i ]   x o r   b [ i ] ) c[](c[i]=a[i] \ xor \ b[i]) c[](c[i]=a[i] xor b[i])字典序最小。

思路

分别对 a [ ] a[] a[] b [ ] b[] b[]建立01字典树,每个节点记录一下左右儿子的叶子节点数 s o n son son。可以设置成等高的树,高度为31。
从两棵树树根同时向下找 n n n次,每次尽量使得二进制相同,不相同就需要加上当前层数的权值,这样获得 n n n个数。
在向下递归的时候,更新 s o n son son,如果 s o n son son为0,相应的删除当前字典树的节点。
最后得到的 n n n个数,排序就是答案。

/*   Author : Rshs
 *   Data : 2019-09-17-20.07
 */
#include<bits/stdc++.h>
using namespace std;
#define FI first
#define SE second
#define LL long long
#define MP make_pair
#define PII pair<int,int>
#define SZ(a) (int)a.size()
const double pai = acos(-1);
const double eps = 1e-10;
const LL mod = 1e9+7;
const int MX = 1e5+5;
int a[MX],b[MX];
int trie[2][MX*35][2];
int nonum[2];
int son[2][MX*35][2]; //左边有几个叶子
vector<int>po;
vector<int>two;
vector<int>ans;
void ins(int ii,int p,int s){
    if(s==SZ(two))return ;
    son[ii][p][two[s]]++;
    if(!trie[ii][p][two[s]]){
        trie[ii][p][two[s]]=nonum[ii],nonum[ii]++;
    }
    ins(ii,trie[ii][p][two[s]],s+1);
}
void  sear(int s,int pa,int pb,int &gg){
    if(s==30) return;
    if(trie[0][pa][0]&&trie[1][pb][0]){
        sear(s+1,trie[0][pa][0],trie[1][pb][0],gg);
        son[0][pa][0]--,son[1][pb][0]--;
        if(son[0][pa][0]==0)trie[0][pa][0]=0;
        if(son[1][pb][0]==0)trie[1][pb][0]=0;
    }
    else if(trie[0][pa][1]&&trie[1][pb][1]) {
        sear(s+1,trie[0][pa][1],trie[1][pb][1],gg);
        son[0][pa][1]--,son[1][pb][1]--;
        if(son[0][pa][1]==0)trie[0][pa][1]=0;
        if(son[1][pb][1]==0)trie[1][pb][1]=0;
    }
    else if(trie[0][pa][0]){
        sear(s+1,trie[0][pa][0],trie[1][pb][1],gg=gg+po[s]);
        son[0][pa][0]--,son[1][pb][1]--;
        if(son[0][pa][0]==0)trie[0][pa][0]=0;
        if(son[1][pb][1]==0)trie[1][pb][1]=0;
    }
    else {
        sear(s+1,trie[0][pa][1],trie[1][pb][0],gg=gg+po[s]);
        son[0][pa][1]--,son[1][pb][0]--;
        if(son[0][pa][1]==0)trie[0][pa][1]=0;
        if(son[1][pb][0]==0)trie[1][pb][0]=0;
    }
}
void Main(int avg){
    /*******************************************************************************************************/
    int n;cin>>n;nonum[0]=nonum[1]=1;ans.clear();
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        int sa=a[i];two.clear();
        while(sa)two.push_back(sa%2),sa/=2;
        while(SZ(two)<30)two.push_back(0);
        reverse(two.begin(),two.end());
        ins(0,0,0);
    }
    for(int i=1;i<=n;i++){
        scanf("%d",&b[i]);
        int sb=b[i];two.clear();
        while(sb)two.push_back(sb%2),sb/=2;
        while(SZ(two)<30)two.push_back(0);
        reverse(two.begin(),two.end());
        ins(1,0,0);
    }
    /*********************************************************************************************************/
    for(int i=1;i<=n;i++){
        int z=0;sear(0,0,0,z);
        ans.push_back(z);
    }
    for(int i=0;i<2;i++)for(int j=0;j<MX;j++)for(int k=0;k<2;k++)if(trie[i][j][k])cout<<i<<' '<<j<<' '<<endl;
    sort(ans.begin(),ans.end());
    int t=ans.back();ans.pop_back();
    for(auto i:ans)cout<<i<<' ';
    cout<<t;
    puts("");
}
int main(){
    po.push_back(1);
    for(int i=1;i<=29;i++) po.push_back((int)(po.back())*2);
    reverse(po.begin(),po.end());
    int cas;cin>>cas;for(int i=1;i<=cas;i++)Main(i);
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值