Dr. Evil Underscores

D - Dr. Evil Underscores

参考:Codeforces Round #613 (Div. 2) Editorial

其实比赛的时候就已经想到了基本上一样的解法,可是最后还是没有写出来...

具体思路就是分治,在二进制中,如果\(a_1{\sim}a_n\),在该位上既有1又有0,说明这一位上的数是躲不掉的,那么这一位上肯定是1,所以在返回的数里加一个1<<i,而对于后面的数则取最小值即可min(dfs(i-1, v0), dfs(i-1, v1))+(1 << i)

要注意的一点:i 要一直算到i<0才结束,因为当i==0是运算的是最后一位的值。

代码:

// Created by CAD on 2020/1/10.
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+5;
ll a[maxn];

int dfs(int i,vector<int> &v)
{
    vector<int> v0,v1;
    if(i<0)    return 0;
    for(auto &j:v)
    {
        if((j>>i)&1) v1.push_back(j);
        else v0.push_back(j);
    }
    if(v0.size()==0) return dfs(i-1, v1);
    if(v1.size()==0) return dfs(i-1, v0);
    return min(dfs(i-1, v0), dfs(i-1, v1))+(1 << i);
}
vector<int> v;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    //    FOPEN;
    int n;
    cin>>n;
    for(int i=1;i<=n;++i)
        cin>>a[i],v.push_back(a[i]);
    cout << dfs(30, v) << endl;
    return 0;
}

原文地址:https://www.cnblogs.com/CADCADCAD/p/12179509.html

时间: 2024-08-30 15:06:45

Dr. Evil Underscores的相关文章

Codeforces Round #613 (Div. 2) D. Dr. Evil Underscores

题目:http://codeforces.com/contest/1285/problem/D 思路:从高位往低位建 \(01\;trie\) 树,从高位 dfs 当只有一个分支,当前位为 \(0\),填法唯一: 当有两个分支,当前位为 \(1\),填法不唯一,则返回较小值: #include<bits/stdc++.h> using namespace std; const int N=1e5+5; int n; int trie[N*31][2]; int cnt; void insert

Codeforces Round #613 (Div. 2) D - Dr. Evil Underscores(思维,位运算)

?? ?? ?? 题意:对于一个数组,求一个数字与数组每个元素异或之后的最大值最小,求这个最大值 又是位运算,,题目给出数组元素范围在2^30以内,二进制最多30位,从最高位开始贪心,如果此位置的数组元素有的是1有的是0,最后肯定取1,否则取0,还有就是分组讨论,因为每个bit位只能满足原数组中一部分元素异或后为1 #define int ll vector<int>a; int solve(vector<int>v,int bit) { if(bit<=0||v.size(

CodeForces 1285D Dr. Evil Underscores

Description CodeForces 1285D 描述 给一个长度为 $n$ 的数组 $a$,试找到一个 $X$,使得 $\max\limits_{1\le i \le n} (a_i \oplus X)$ 最小,输出这个最小值即可. 输入 第一行一个数 $n(1 \le n \le 10^5)$,接下来第二行 $n$ 个数表示 $a$ 数组 $(0\le a_i \le 2^{30} - 1)$. 输出 一行一个数,表示所求最小值. 样例 输入1 31 2 3 输出1 2 输入2 21

CF1285D Dr. Evil Underscores

挂个链接 Description: 给你 \(n\) 个数 \(a_1,a_2,--,a_n\) ,让你找出一个 \(x\) ,使 \(x\) 分别异或每一个数后得到的 \(n\) 个结果的最大值最小. Solution: 设 \(x\) 为题中所说, \(m\) 为 \(max{x^a_i}\) : 构建 \(01Trie\) ,将所有数的二进制形式存到 \(01Trie\) 上,对于第 \(k\) 位,存在两种情况: 一是都是0或都是1,这样只要使 \(x\) 的第 \(k\) 位为1或0,

Codeforces 1285D Dr. Evil Underscores(字典树,dp)

传送门 题意: 有一个长度为 \(n\ (1\leq n\leq 10^5)\)的整数序列 \(a_1,\cdots,a_n\ \ (0\leq a_i\leq 2^{30}-1)\),你需要找到一个非负整数 \(X\) 使得 \(\max(a_i\oplus X)\)最小,其中 \(\oplus\) 为按位异或运算. 输入这个序列,输出\(\max(a_i\oplus X)\)的最小值. 思路: 1.数组中的每个数的二进制下的某位(第k位)都是0或者是1,那么x的第k位取值是0或者1,使得答案

Head First HTML5 Programming笔记--chapter2 介绍Javascript和DOM

你已经了解了HTML标记(也称为结构),而且知道了CSS样式(也称为表示),剩下的就是Javascript(也称为行为). JavaScript的工作方式 1. 编写 你创建HTML标记和JavaScript代码,并把它们放在文件中,比如说index.html和index.js(或者,也可以都放在HTML文件中). 2. 加载 浏览器获取并加载你的页面,从上到下解析它的内容.遇到JavaScript时,浏览器会解析代码,检查它的正确性,然后执行代码.浏览器还会建立HTML的一个内部模型,称为DO

CF 862C Mahmoud and Ehab and the xor(异或)

题目链接:http://codeforces.com/problemset/problem/862/C 题目: Mahmoud and Ehab are on the third stage of their adventures now. As you know, Dr. Evil likes sets. This time he won't show them any set from his large collection, but will ask them to create a n

E - Mahmoud and Ehab and the bipartiteness CodeForces - 862B (dfs黑白染色)

Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees. A tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in su

&lt;Dr.Elephant&gt;

Why Dr.Elephant? Most of Hadoop optimization tools out there, but they are focused on simplifying the deploy and managment of Hadoop clusters. Very few tools are designed to help Hadoop users optimize their flows. Dr.Elephant supports Hadoop with a v