XOR array

Perhaps a better reading experience

\(\mathcal{Description}\)

Give you two of length \ (n-\) array \ (a, b \)
you need to \ (a b, \) sorted in some manner two arrays are
then allowed \ (c_i = a_i \ xor \ b_i \) , you make \ (C \) is lexicographically smallest
requested output \ (C \) the array
\ (n \ leq200000 \ a_i, b_i \ leq 2 ^ {30} \)

\(\mathcal{Solution}\)

The \ (a, b \) number two arrays are placed in a \ (01Trie \) inside the tree
and note the position of each point of the string after the number
and configuration considerations \ (C \)
two \ (Trie \) trees began to run together, from \ (2 ^ {30} \) began to consider
to consider both sides up to the exclusive-oR \ (0 \) case, consider both sides up to the exclusive oR \ (1 \) is
so finish \ (2 ^ 0 \) of a point obtained after the \ (C \) , and must be optimal

Next is the issue of skills
if \ (c \) for each element separately considered, that every match once again, even though I changed it to \ (bfs \) and carried out some optimization, but still \ (T (50pts) \)

Consider again \ (DFS \) handles all the answers
at every position up to first consider the exclusive-OR \ (0 \) , then it is considered exclusive or \ (1 \) case
are then each time after the change point series minus the number of \ (1 \)
This can be passed

\(\mathcal{Code}\)

50 points Code

/*******************************
Author:Morning_Glory
LANG:C++
Created Time:2019年09月16日 星期一 09时45分46秒
*******************************/
#include <cstdio>
#include <fstream>
#include <algorithm>
using namespace std;
const int len = 30;
const int maxn = 3000006;
//{{{cin
struct IO{
    template<typename T>
    IO & operator>>(T&res){
        res=0;
        bool flag=false;
        char ch;
        while((ch=getchar())>'9'||ch<'0')   flag|=ch=='-';
        while(ch>='0'&&ch<='9') res=(res<<1)+(res<<3)+(ch^'0'),ch=getchar();
        if (flag)   res=~res+1;
        return *this;
    }
}cin;
//}}}
int n,a,b,h,t,k,cnt;
int mi[len+1],res[maxn];
int q[3][maxn];//q[0] -> 哪一位 q[1] -> na q[2] -> nb
struct Trie{
    int tot=1;
    int num[maxn];
    int ch[maxn][2];
    //{{{insert
    void insert (int s)
    {
        int now=1;
        for (int i=len;i>=0;--i){
            bool to=s&mi[i];
            if (!ch[now][to]) ch[now][to]=++tot;
            now=ch[now][to];
            ++num[now];
        }
    }
    //}}}
}A,B;
//{{{dfs
void dfs(int x,int y,int S,int k)
{
    if(x) --A.num[x],--B.num[y];
    if(k<0) {   res[++cnt]=S;return;}
    while(A.num[A.ch[x][0]]&&B.num[B.ch[y][0]])  dfs(A.ch[x][0],B.ch[y][0],S,k-1);
    while(A.num[A.ch[x][1]]&&B.num[B.ch[y][1]])  dfs(A.ch[x][1],B.ch[y][1],S,k-1);
    while(A.num[A.ch[x][0]]&&B.num[B.ch[y][1]])  dfs(A.ch[x][0],B.ch[y][1],S|mi[k],k-1);
    while(A.num[A.ch[x][1]]&&B.num[B.ch[y][0]])  dfs(A.ch[x][1],B.ch[y][0],S|mi[k],k-1);
}
//}}}
int main()
{
    mi[0]=1;
    for (int i=1;i<=len;++i)    mi[i]=mi[i-1]<<1;
    cin>>n;
    for (int i=1;i<=n;++i)  cin>>a,A.insert(a);
    for (int i=1;i<=n;++i)  cin>>b,B.insert(b);
    dfs(1,1,0,30);
    sort(res+1,res+n+1);
    for (int i=1;i<=n;++i)  printf("%d ",res[i]);
    return 0;
}

100 min Code

/*******************************
Author:Morning_Glory
LANG:C++
Created Time:2019年09月16日 星期一 09时45分46秒
*******************************/
#include <cstdio>
#include <fstream>
#include <algorithm>
using namespace std;
const int len = 30;
const int maxn = 3000006;
//{{{cin
struct IO{
    template<typename T>
    IO & operator>>(T&res){
        res=0;
        bool flag=false;
        char ch;
        while((ch=getchar())>'9'||ch<'0')   flag|=ch=='-';
        while(ch>='0'&&ch<='9') res=(res<<1)+(res<<3)+(ch^'0'),ch=getchar();
        if (flag)   res=~res+1;
        return *this;
    }
}cin;
//}}}
int n,a,b,h,t,k,cnt;
int mi[len+1],res[maxn];
int q[3][maxn];//q[0] -> 哪一位 q[1] -> na q[2] -> nb
struct Trie{
    int tot=1;
    int num[maxn];
    int ch[maxn][2];
    //{{{insert
    void insert (int s)
    {
        int now=1;
        for (int i=len;i>=0;--i){
            bool to=s&mi[i];
            if (!ch[now][to]) ch[now][to]=++tot;
            now=ch[now][to];
            ++num[now];
        }
    }
    //}}}
}A,B;
//{{{dfs
void dfs(int x,int y,int S,int k)
{
    if(x) --A.num[x],--B.num[y];
    if(k<0) {   res[++cnt]=S;return;}
    while(A.num[A.ch[x][0]]&&B.num[B.ch[y][0]])  dfs(A.ch[x][0],B.ch[y][0],S,k-1);
    while(A.num[A.ch[x][1]]&&B.num[B.ch[y][1]])  dfs(A.ch[x][1],B.ch[y][1],S,k-1);
    while(A.num[A.ch[x][0]]&&B.num[B.ch[y][1]])  dfs(A.ch[x][0],B.ch[y][1],S|mi[k],k-1);
    while(A.num[A.ch[x][1]]&&B.num[B.ch[y][0]])  dfs(A.ch[x][1],B.ch[y][0],S|mi[k],k-1);
}
//}}}
int main()
{
    mi[0]=1;
    for (int i=1;i<=len;++i)    mi[i]=mi[i-1]<<1;
    cin>>n;
    for (int i=1;i<=n;++i)  cin>>a,A.insert(a);
    for (int i=1;i<=n;++i)  cin>>b,B.insert(b);
    dfs(1,1,0,30);
    sort(res+1,res+n+1);
    for (int i=1;i<=n;++i)  printf("%d ",res[i]);
    return 0;
}

If not quite understand where to put it or there is an error, please correct me
if you like, you may wish to point a collection of praise about it

Guess you like

Origin www.cnblogs.com/Morning-Glory/p/11528014.html
XOR