CF1285D Dr. Evil Underscores

Hanging links

Description:

You \ (n \) the number \ (A_1, A_2, ......, A_N \) , so that you find a \ (x \) , so \ (x \) are exclusive or every number obtained after \ ( n-\) minimum and maximum of the results.

Solution:

Set \ (X \) is mentioned in the title, \ (m \) of \ (a_i max {X} ^ \) :

Construction \ (01Trie \) , all in the form of binary numbers stored to \ (01Trie \) on, for the first \ (K \) bits, there are two cases:

  • First, both are 0 or 1, so that the long \ (X \) of \ (k \) bit is 1 or 0, it is possible to \ (m \) k-th bit is 0. The greedy strategy , the high is 0 the number must be younger than the high for the number 1, no matter how low.

  • Second, it has a 0 and 1, then separate dfs, whichever is greater. This situation is not well understood, you can see the specific code.

Code:

I did not build \ (Trie \) , but directly dfs.

\ (dfs (v [], k) \) represents \ (V \) minimum number of \ (m \) , and has been found to the second \ (K \) bit of. (Shovel blackboard, draw the focus !!!


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const ll N = 1e5+1;
ll n,ans;

ll dfs(vector<ll> v,ll k)
{
    vector<ll> v1,v2;
    if(k<0) return 0;
    for(int i=0;i<v.size();++i)
    {
        if((v[i]>>k)&1) v1.push_back(v[i]);
        else v2.push_back(v[i]);
    }
    if(v1.empty()||v2.empty()) return dfs(v,k-1);//01Trie 只有一个分支 
    ll d1=dfs(v1,k-1),d2=dfs(v2,k-1);
    return min(max(d1,d2+(1<<k)),max(d1+(1<<k),d2));//有两个分支
}

int main()
{
    vector<ll> v; 
    scanf("%lld",&n);
    while(n--)
    {
        ll tmp;scanf("%lld",&tmp);
        v.push_back(tmp);
    }
    printf("%lld\n",dfs(v,30));
    return 0;
}

Guess you like

Origin www.cnblogs.com/oierwyh/p/12189209.html