标签:font stdin admin out inpu cstring algo cli pac
思路:对每位二进制进行判断,如果当前位的二进制都为0或者1,则这个位对答案贡献为0,如果当前为有0有1则这个位对答案贡献(1 << bit),然后对当前位为0和为1的分成两个集合进行分治,所有答案取min。
1 #include <cstdio> 2 #include <iostream> 3 #include <cstdio> 4 #include <algorithm> 5 #include <functional> 6 #include <set> 7 #include <vector> 8 #include <queue> 9 #include <cstring> 10 #include <stack> 11 #include <climits> 12 13 using namespace std; 14 15 #define ll long long 16 #define pb push_back 17 #define fi first 18 #define se second 19 20 int process(int b, vector<int >& v){ 21 if(b < 0) return 0; 22 vector<int > v0, v1; 23 for(auto x : v){ 24 if((x >> b) & 1) v1.pb(x); 25 else v0.pb(x); 26 } 27 if(v0.size() == 0) return process(b - 1, v1); 28 if(v1.size() == 0) return process(b - 1, v0); 29 return min(process(b - 1, v0), process(b - 1, v1)) + (1 << b); 30 } 31 32 void solve(){ 33 34 int n; 35 cin >> n; 36 vector<int > a(n); 37 for(auto& in : a) cin >> in; 38 int ans = process(30, a); 39 cout << ans << endl; 40 } 41 42 int main(){ 43 44 // freopen("C:\\Users\\admin\\Desktop\\input.txt", "r", stdin); 45 // freopen("C:\\Users\\admin\\Desktop\\output.txt", "w", stdout); 46 ios::sync_with_stdio(false); 47 cin.tie(0); 48 cout.tie(0); 49 solve(); 50 51 return 0; 52 } 53
CF1285D Dr. Evil Underscores (分治 + 二进制)
标签:font stdin admin out inpu cstring algo cli pac
原文地址:https://www.cnblogs.com/SSummerZzz/p/12836594.html