题解 | #牛牛的xor#

牛牛的xor

https://ac.nowcoder.com/acm/problem/21298

将数字中含1的地方尽量大,从29开始因为1<<29是1e9的最高位。

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

int main() {
    int n;
    cin >> n;
    vector<int> a(n, 0);
    for (int i = 0; i < n; ++i) cin >> a[i];
    sort(a.begin(), a.end());
    a = vector<int> (a.rbegin(), a.rend());
    int ans = 0;
    //for (auto i : a) cout << i << " ";cout << endl;
    for (auto itrm : a)
        for (int i = 29; i >= 0; --i)
        {
            int t = 1 << i;
            if (!(ans & t) && itrm >= t)
            {
                ans |= t; 
                itrm -= t;
            }
        }
    cout << ans << endl;
}
全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务