Codeforces Round #613 (Div. 2) D. Dr. Evil Underscores
作者:互联网
贪心 + 分治 + 位运算
从高到低逐位考虑,分两种情况:
1、目前集合中当前位只有0或只有1,此位贡献为0
2、目前集合中当前位(第k位)同时存在0和1,那么此位对这个集合的贡献为 1 << k,然后以0和1划分当前集合的子集,分别求子集的最小贡献
#include<bits/stdc++.h>
using namespace std;
#define fr first
#define se second
typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 0X3f3f3f3f, N = 1e5 + 10, MOD = 1e9 + 7;
vector<int> v;
int get_min(int k, vector<int> & cur) {
if (k < 0) return 0;
vector<int> a, b;
for (int i = 0; i < cur.size(); i++) {
if (cur[i] >> k & 1) a.push_back(cur[i]);
else b.push_back(cur[i]);
}
if (a.size() == 0) return get_min(k - 1, b);
if (b.size() == 0) return get_min(k - 1, a);
return min(get_min(k - 1, a) + (1 << k), get_min(k - 1, b) + (1 << k));
}
void work() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
v.push_back(x);
}
cout << get_min(31, v);
}
signed main() {
work();
return 0;
}
标签:return,cur,min,int,613,get,Underscores,Codeforces,long 来源: https://www.cnblogs.com/xhy666/p/16425466.html