three arrays(hdu 6625)

Title link

The first question of the boyfriend automatic question machine

Original idea:

  I originally wanted to build a dictionary tree from the a array in binary, and then use the b array to perform optimal matching, but I couldn't think of how to deal with the problem of repetition.

later:

  After a while of kan, wan, and ti, I found this:

    Build two dictionary trees a and b respectively for the two arrays according to the binary and then perform the optimal matching for each search.

  See the code in detail

#include <bits / stdc ++. h>
 using  namespace std;
 const  int maxn = 3e6 + 10 ;
 #define ios std :: ios :: sync_with_stdio (false)
 struct node 
{ 
    int ci, di [ 2 ]; /// c means How many times the current point is used, di 

stores subscripts whose current bits are 0 and 1 }; node treea [maxn], treeb [maxn]; /// Two trees 
int cnta, cntb; /// Two trees The maximum subscript of 
void init () /// initialization 
{
     for ( int i = 0 ; i <= cnta; i ++ ) { 
        treea [i] .ci= 0;
        treea[i].di[0] = treea[i].di[1] = 0;
    }
    for(int i = 0 ; i <= cntb ; i ++){
        treeb[i].ci = 0;
        treeb[i].di[0] = treeb[i].di[1] = 0;
    }
}
void insert1(int a)
{
    int dir = 1;///初始坐标
    for(int i = 32 ; i >= 0 ; i- ) {
         int t = (a >> i & 1 );
         if (treea [dir] .di [t] == 0 ) { /// If the current bit is t has not generated a 
            treea [ dir] .di [t] = ++ cnta; 
        } 
        dir = treea [dir] .di [t]; /// Change to this coordinate 
        treea [dir] .ci ++; /// Usage degree + 1 
    } 
} 
void insert2 ( int b) /// Ibid. 
{
     int dir = 1 ;
     for ( int i = 32 ; i> = 0 ; i --){
        int t = (b >> i & 1);
        if(treeb[dir].di[t] == 0){
            treeb[dir].di[t] = ++cntb;
        }
        dir = treeb[dir].di[t];
        treeb[dir].ci ++;
    }
}
int get_ans()
{
    int ca = 1 , cb = 1;///两个初始坐标
    int ans = 0;
    for(int i = 32 ; i >= 0; i- ) {
         int zeroa = 0 , zerob = 0 , onea = 0 , oneb = 0 ; / // This 01 Yes Yes Yes Yes 
        if (treea [ca] .di [ 0 ]! = 0 && treea [ treea [ca] .di [ 0 ]]. ci> 0 ) zeroa = 1 ;
         if (treea [ca] .di [ 1 ]! = 0 && treea [treea [ca] .di [ 1 ]]. ci> 0 ) onea = 1 ;
         if (treeb [cb] .di [ 0 ]! = 0 && treeb [treeb [cb] .di [0]].ci > 0)zerob = 1;
        if(treeb[cb].di[1] != 0 && treeb[treeb[cb].di[1]].ci > 0)oneb = 1;
        if(onea && oneb){
            ca = treea[ca].di[1];
            cb = treeb[cb].di[1];
        }
        else if(zeroa && zerob){
            ca = treea[ca].di[0];
            cb = treeb[cb].di[0];
        }
        else if(zeroa && oneb){
            ca = treea[ca].di[0];
            cb = treeb[cb].di[1];
            ans += (1 << i);
        }
        else if(zerob && onea){
            ca = treea[ca].di[1];
            cb = treeb[cb].di[0];
            ans += (1 << i);
        }
        treea[ca].ci --;
        treeb[cb].ci --;
    }
    return ans;
}
int a[maxn] , b[maxn] , c[maxn];
signed main()
{
    ios;
    cin.tie(0);///
    int t;
    cin >> t;
    while(t --)
    {
        int n;
        cin >> n;
        cnta = 1;
        cntb = 1;
        for(int i = 1 ; i <= n ; i ++) cin >> a[i] , insert1(a[i]);
        for(int i = 1 ; i <= n ; i ++) cin >> b[i] , insert2(b[i]);
        for(int i = 1 ; i <= n ; i ++){
            c[i] = get_ans();
        }
        sort(c + 1 , c + 1 + n);
        for(int i = 1 ; i <= n ; i ++){
            cout << c[i];
            if(i != n)cout << ' ';
        }
        cout << '\n';
        init();
    }
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/GoodVv/p/12729659.html