2019HDU集訓——6625 three arrays

大致題意:給兩個數組a,b,現在可以把a,b分別重排,也就是a,b中的元素可以任意改變位置。然後把a,b中每個位置的數異或起來。要求最終得到的數 組成的 數組中元素的字典序最小。(感覺自己描述地好垃圾,算了,看了題的都知道是什麼意思)。

很明顯,要用到字典樹,因爲字典樹中的“前綴”性質就簡直是這道題的利器。思路:把a數組中的數分解成二進制去建一棵字典樹。b也同樣建一棵。然後根據在紙上畫出來的字典樹,很明顯,就是對於兩個數組中的每個數,去對方字典樹上找一個還沒有被使用的、同時公共前綴最長的數就行了,也就是說,我們需要同時在兩棵字典樹上dfs。 

瞎扯部分:本來我是想着只建一棵樹,然後用另一個數組上的數字去字典樹上貪前綴就行了,但是在實際的代碼實現中遇到了困難/(ㄒoㄒ)/~~,所以就參考了一下別人的實現方式?,而且這道題時間卡的比較緊,就連T中每次的初始化都不能直接memset字典樹的全部空間。。。。。還有從高位開始建樹,就可以把所有的數在前面補全0,也就是所有的數的長度就可以一致了,這個方法很漂亮,這在貪心前綴的時候可以節省了很多無聊的代碼。

所以,這樣建成兩棵字典樹,同時跑dfs,或者用while優化實現dfs。

最後,代碼:

#include<bits/stdc++.h>
using namespace std;
const int maxn=3e6+10000;
int trie1[maxn][2];
int sum1[maxn];
int trie2[maxn][2];
int sum2[maxn];
int cur;
int tot;
void Insert1(int a)
{
    int p=0;
    for(int i=29;i>=0;--i)
    {
        int x=(a>>i&1)?1:0;
        if(!trie1[p][x])
        {
            trie1[p][x]=++cur;
            memset(trie1[cur],0,sizeof trie1[cur]);
        }
        p=trie1[p][x];
        ++sum1[p];
    }
}
void Insert2(int a)
{
    int p=0;
    for(int i=29;i>=0;--i)
    {
        int x=(a>>i&1)?1:0;
        if(!trie2[p][x])
        {
            trie2[p][x]=++tot;
            memset(trie2[tot],0,sizeof trie2[tot]);
        }
        p=trie2[p][x];
        ++sum2[p];
    }
}
priority_queue<int,vector<int>,greater<int> >q;
void solve(int n)
{
    while(n--)
    {
        int x=0;
        int f1=0,f2=0;
        for(int i=29;i>=0;--i)
        {
            if(sum1[trie1[f1][0]]&&sum2[trie2[f2][0]])
            {
                --sum1[trie1[f1][0]];
                --sum2[trie2[f2][0]];
                f1=trie1[f1][0];
                f2=trie2[f2][0];
            }
            else if(sum1[trie1[f1][1]]&&sum2[trie2[f2][1]])
            {
                --sum1[trie1[f1][1]];
                --sum2[trie2[f2][1]];
                f1=trie1[f1][1];
                f2=trie2[f2][1];
            }
            else if(sum1[trie1[f1][1]]&&sum2[trie2[f2][0]])
            {
                --sum1[trie1[f1][1]];
                --sum2[trie2[f2][0]];
                f1=trie1[f1][1];
                f2=trie2[f2][0];
                x^=(1<<i);
            }
            else
            {
                --sum1[trie1[f1][0]];
                --sum2[trie2[f2][1]];
                f1=trie1[f1][0];
                f2=trie2[f2][1];
                x^=(1<<i);
            }
        }
        q.push(x);
    }
    while(!q.empty())
    {
        printf("%d",q.top());

        q.pop();
        if(!q.empty())
        printf(" ");
    }
    printf("\n");
}
int main()
{
    //ios::sync_with_stdio(false);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        cur=0;
        tot=0;
        memset(trie1[0],0,sizeof trie1[0]);
        memset(trie2[0],0,sizeof trie2[0]);
        int n,a;
        scanf("%d",&n);
        for(int i=0;i<n;++i)
        {
            scanf("%d",&a);
            Insert1(a);
        }
        for(int i=0;i<n;++i)
        {
            scanf("%d",&a);
            Insert2(a);
        }
        solve(n);
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章