zoukankan      html  css  js  c++  java
  • three arrays

    three arrays

    字典树上贪心

    #include<bits/stdc++.h>
    using namespace std;
    int trie[3000005][2][2];
    int siz[3000004][2];
    int A[31];
    int tot[2];
    bool ed[3000005][2];
    int val[3000004][2];
    int N;
    void init()
    {
        tot[0]=tot[1]=1;
        for(int i=0; i<=30*N; i++)
        {
            val[i][0]=val[i][1]=0;
            ed[i][0]=ed[i][1]=0;
            trie[i][1][1]=trie[i][0][1]=trie[i][0][0]=trie[i][1][0]=0;
            siz[i][1]=siz[i][0]=0;
        }
    
    }
    void insert(int x,bool f)
    {
        int k=0;
        int y=x;
        while(x)
        {
            A[k++]=x%2;
            x/=2;
        }
        while(k<31)A[k++]=0;
        int t=1;
        int j=30;
        int len=0;
        while(j>=0)
        {
            ++siz[t][f];
            if(trie[t][A[j]][f]==0)
            {
                trie[t][A[j]][f]=++tot[f];
            }
            t=trie[t][A[j]][f];
    
            j--;
          //  cout<<t<<"t"<<endl;
            len++;
        }
        //cout<<len<<" leN
    ";
        ++siz[t][f];
        ed[t][f]=1;
        val[t][f]=y;
        //cout<<t<<'
    ';
    
    }
    int query()
    {
        int t0=1,t1=1;
        int len=0;
        while(!ed[t0][0]&&!ed[t1][1])
        {
            --siz[t0][0];
            --siz[t1][1];
           // cout<<t0<<t1<<endl;//siz[t0][0]<<siz[t1][1]<<endl;
            if(siz[trie[t0][0][0]][0]&&siz[trie[t1][0][1]][1])
            {
                t0=trie[t0][0][0];
                t1=trie[t1][0][1];
            }
            else if(siz[trie[t0][1][0]][0]&&siz[trie[t1][1][1]][1])
            {
                t0=trie[t0][1][0];
                t1=trie[t1][1][1];
            }
            else
            {
                if(siz[trie[t0][1][0]][0])
                    t0=trie[t0][1][0];
                else t0=trie[t0][0][0];
                if(siz[trie[t1][1][1]][1])
                    t1=trie[t1][1][1];
                else t1=trie[t1][0][1];
    
            }
          //  ++len;
        }
        //cout<<len<<"LEN"<<'
    ';
        siz[t0][0]--;
        siz[t1][1]--;
        //cout<<t0<<t1<<'
    ';
       //cout<<ed[t0][0]<<' '<<ed[t1][1]<<'
    ';
        return val[t0][0]^val[t1][1];
    }
    int T;
    int a,b;
    int C[100005];
    int main()
    {
        //freopen("1.in","r",stdin);
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&N);
            init();
            for(int i=0; i<N; i++)
            {
                scanf("%d",&a);
                insert(a,0);
            }
            for(int i=0; i<N; i++)
            {
                scanf("%d",&b);
                insert(b,1);
            }
            for(int i=0;i<N;i++){
                C[i]=query();
            }
            sort(C,C+N);
            for(int i=0; i<N; i++)
            {
                cout<<C[i]<<((i!=N-1)?' ':'
    ');
            }
        }
    
    }
  • 相关阅读:
    Java RunTime Environment (JRE) or Java Development Kit (JDK) must be available in order to run Eclipse. ......
    UVA 1597 Searching the Web
    UVA 1596 Bug Hunt
    UVA 230 Borrowers
    UVA 221 Urban Elevations
    UVA 814 The Letter Carrier's Rounds
    UVA 207 PGA Tour Prize Money
    UVA 1592 Database
    UVA 540 Team Queue
    UVA 12096 The SetStack Computer
  • 原文地址:https://www.cnblogs.com/liulex/p/11314613.html
Copyright © 2011-2022 走看看