zoukankan      html  css  js  c++  java
  • Codeforces-613D Dr. Evil Underscores

    Description

    Today, as a friendship gift, Bakry gave Badawy nn integers (a_1,a_2,…,a_n) and challenged him to choose an integer (X) such that the value (mathop{max}limits_{1≤i≤n}(ai⊕X)) is minimum possible, where ⊕ denotes the bitwise XOR operation.

    As always, Badawy is too lazy, so you decided to help him and find the minimum possible value of (mathop{max}limits_{1≤i≤n}(ai⊕X)).

    Input

    The first line contains integer (n (1≤n≤10^5)).

    The second line contains (n) integers $ a_1,a_2,…,a_n(0≤a_i≤2^{30}−1).$

    Output

    Print one integer — the minimum possible value of (mathop{max}limits_{1≤i≤n}(ai⊕X))

    Examples

    input

    3
    1 2 3
    

    output

    2
    

    input

    2
    1 5
    

    output

    4
    

    Note

    In the first sample, we can choose (X=3).

    In the second sample, we can choose $ X=5$.

    题意

    给定n个数,求一个数x使得(mathop{max}limits_{1≤i≤n}(ai⊕X))最小,并输出(mathop{max}limits_{1≤i≤n}(ai⊕X))

    题解

    我们从最高位向低位分治,对于每一位,(O(n))扫一遍所有的数,如果这一位所有数都是1,或者所有数都是零,我们就可以让这一位对答案不产生贡献,否则这一位产生的贡献是不可避免的,我们只需要看后面的数产生的贡献哪个比较小就可以了。这样我们把这一位是1的,和这一位是0的分成两个集合,分别递归下去,取返回值较小的,加上(1 << d)即可。

    代码

    #include <bits/stdc++.h>
    using namespace std;
    const int N = 1e5 + 50;
    vector<int> v;
    typedef long long ll;
    ll dfs(int d, vector<int> v) {
        if (v.size() == 0 || d < 0) return 0;
        vector<int> v1, v0;
        for (int i = 0; i < v.size(); i++) {
            if ((v[i] >> d) & 1) v1.push_back(v[i]);
            else v0.push_back(v[i]);
        }
        if (v0.size() == 0) return dfs(d - 1, v1);
        else if (v1.size() == 0) return dfs(d - 1, v0);
        else return min(dfs(d - 1, v1), dfs(d - 1, v0)) + (ll)(1 << d);
    }
    int main() {
        int n;
        scanf("%d", &n);
        int maxx = 0;
        for (int i = 1; i <= n; i++) {
            int x;
            scanf("%d", &x);
            v.push_back(x);
            maxx = max(maxx, x);
        }
        int cnt = 0;
        while (maxx) {
            maxx >>= 1;
            cnt++;
        }
        printf("%lld
    ", dfs(cnt, v));
        return 0;
    }
    
  • 相关阅读:
    hdu1238 Substrings
    CCF试题:高速公路(Targin)
    hdu 1269 迷宫城堡(Targin算法)
    hdu 1253 胜利大逃亡
    NYOJ 55 懒省事的小明
    HDU 1024 Max Sum Plus Plus
    HDU 1087 Super Jumping! Jumping! Jumping!
    HDU 1257 最少拦截系统
    HDU 1069 Monkey and Banana
    HDU 1104 Remainder
  • 原文地址:https://www.cnblogs.com/artoriax/p/12183272.html
Copyright © 2011-2022 走看看