标签:dfs cores https cti pac else 最大 bit ring
题目大意:给一组数,找一个x使的x和所有给的数的异或值的最大值max最小,求这个max
解题思路:参考了https://codeforces.com/blog/entry/72950。从每个数的二进制第30位开始到第1位,如果这一位对于所有的数来说都是0或者都是1,那么max在这一位就可以取0,如果这一位既有0又有1,那么这一位一定是1,对于这种情况,x的取值就有两种,对于x取1和x取0选出一个可以使后面位最小的(用递归)。
code:
#include <iostream> #include <string> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <queue> #include <functional> #include <map> #include <set> #include <stack> #define FT(a, b) memset(a, b, sizeof(a)) #define FAT(a) memset(a, 0, sizeof(a)) using namespace std; typedef long long ll; const int M = 1e5 + 10; const int INF = 0x3f3f3f3f; const int mod = 998244353; vector<ll> v; ll dfs(vector<ll> &v, int bit) { vector<ll> v0, v1; if (bit < 0 || v.size() == 0) return 0; for (ll &j : v) { if ((j >> bit) & 1) v1.push_back(j); else v0.push_back(j); } if (v1.size() == 0) return dfs(v0, bit - 1); if (v0.size() == 0) return dfs(v1, bit - 1); return min(dfs(v0, bit - 1), dfs(v1, bit - 1)) + (1<<bit); } int main() { #ifdef ONLINE_JUDGE #else freopen("/home/code/c++/in.txt", "r", stdin); #endif int n; v.clear(); scanf("%d", &n); for (int i = 0; i < n; i++) { ll x; scanf("%lld", &x); v.push_back(x); } ll ans = dfs(v, 30); printf("%lld\n", ans); return 0; }
Codeforces Round #613 (Div. 2)
标签:dfs cores https cti pac else 最大 bit ring
原文地址:https://www.cnblogs.com/ignorance/p/12181193.html