CF1285 --- Dr. Evil Underscores

题干

Today as a friendship gift, Bakry gave Badawy \(n\) integers \(a_1,a_2, \cdots ,a_n\) and challenged him to choose an integer \(X\) such that the value \(\mathop{max}\limits_{1\leq i\leq n}(a_i \oplus X)\) is minimum possible, where \(\oplus\) denotes the bitwise XOR operation.

\(\mathcal{Input}\)

The first line contains integer \(n (1\leq n\leq 10^5)\).

The second line contains \(n\) integers \(a_1,a_2, \cdots ,a_n\) \((0\leq a_i\leq 2^{30}−1).\)

\(\mathcal{Output}\)

Print one integer — the minimum possible value of \(\mathop{max}\limits_{1\leq i\leq n}(a_i \oplus X)\)

\(\mathcal{Example}\)

\(Case_1\)

\(Input\)

3

1 2 3

\(Output\)

2

\(Case_2\)

\(Input\)

2

1 5

\(Output\)

4

\(\mathcal{Note}\)

In the first sample, we can choose \(X=3\).

In the second sample, we can choose \(X=5\).

\(\mathcal{Tag}\)

bitmasks divide and conquer dfs and similar dp *1800

思路分析

 这题就是选取一个\(X\)值,让序列\(a_1,a_2, \cdots ,a_n\)异或他之后的最大值最小。实际上,这个题目是有两个关键词。

  • 对一个序列进行异或,或者区间异或 \(\rightarrow\) \(\mathcal{Trie\; Tree}\)(字典树)
  • 最大值最小问题 \(\rightarrow\) binary search or conquer and divide.

暴力想法

 这题想的时候,由于信息熵肯定要遍历序列的,然后考虑序列中的最大值,我估计是\(\mathcal{O}(nlog_n)\)的复杂度,因此我直接考虑的是进行二分。但是没有找到分治的split点。因此没有想出较好的方法。

算法优化

 这题主要抓住一个问题,即我们要让最大值最小,因此得考虑最坏情况下的最小值。因此抽象来,就是我们不断做出策略,求解最终策略下最坏情况的最大值。(为什么是不断做出策略呢?)

 有关异或的性质,我们知道当该序列中,某位上所有都为0或1时,我们可以通过制定\(X\)在这位的值,让他在异或序列时,最终的结果都为0.我们假设序列中某\(bit\)为1的所有数为数组\(a_{on}\),某\(bit\)为0的所有数为数组\(a_{off}\).因此当\(a_{on},a_{off}\)都不为空时,该\(bit\)位的值为\(2^{bit}\)。因为不论\(X\)在该位如何设置0或1,一定存在一个数组,让\(X\)异或之后该位的值为\(2^bit\).又因为我们要进行贪心寻找---从高位往地位找,这样才能保证我们在某数组为空时舍弃另一数组的正确性。在都不为空时进行分治。总结如下:

\[dfs(a, bit) = \left\{ \begin{array}[ll]
1dfs(a_{on}, bit-1) &if\; a_{off} = \emptyset\\
dfs(a_{off}, bit-1) &if\; a_{on} = \emptyset\\
min(dfs(a_{on}, bit-1), dfs(a_{off}, bit-1)) + (1 << bit) &if\; a_{off} \not= \emptyset\; and\; a_{on} \not= \emptyset \\
\end{array}\right.
\]

 写出这个表达式之后,求解就非常简单了~ 代码如下

代码

#include<bits/stdc++.h>
using namespace std; using VI = vector<int>; int dfs(VI& a, int bit = 30){
if (bit < 0 || a.empty()) return 0;
VI on, off; for (int x : a){
if ((x >> bit) & 1) on.push_back(x);
else off.push_back(x);
} if (on.empty()) return dfs(off, bit - 1);
if (off.empty()) return dfs(on, bit - 1); return min(dfs(on, bit - 1), dfs(off, bit - 1)) + (1 << bit);
} int main(){
int n; cin >> n;
VI a(n);
for (auto& e : a) cin >> e;
cout << dfs(a) << endl;
return 0;
}

 这里没有考虑到 \(\mathcal{Trie\; Tree}\)(字典树)的解法,后续可以专门讨论下。

CF1285 --- Dr. Evil Underscores的更多相关文章

  1. DFS-B - Dr. Evil Underscores

    B - Dr. Evil Underscores Today, as a friendship gift, Bakry gave Badawy nn integers a1,a2,…,ana1,a2, ...

  2. codeforces 1285D. Dr. Evil Underscores(字典树)

    链接:https://codeforces.com/problemset/problem/1285/D 题意:给n个数a1,a2,a3.....an,找到一个数X,使得X 异或所有的ai ,得到的ma ...

  3. CF1285D Dr. Evil Underscores

    挂个链接 Description: 给你 \(n\) 个数 \(a_1,a_2,--,a_n\) ,让你找出一个 \(x\) ,使 \(x\) 分别异或每一个数后得到的 \(n\) 个结果的最大值最小 ...

  4. C - Dr. Evil Underscores CodeForces - 1285D 二进制

    题目大意:n个数,任意整数x对这n个数取异或值,然后使最大值最小. 思路:数据范围最大为pow(2,30);所以考虑二进制的话,最多有30位.对于某一位d,然后考虑数组v中每一个元素的d为是0还是1, ...

  5. codeforces每日一题1-10

    目录: 1.1093D. Beautiful Graph(DFS染色) 2.514C - Watto and Mechanism(Tire) 3.69E.Subsegments(STL) 4.25C. ...

  6. Codeforces Round #613 (Div. 2) A-E简要题解

    contest链接:https://codeforces.com/contest/1285 A. Mezo Playing Zoma 签到 #include<iostream> #incl ...

  7. Codeforces 862A Mahmoud and Ehab and the MEX

    传送门:CF-862A A. Mahmoud and Ehab and the MEX time limit per test 2 seconds memory limit per test 256 ...

  8. CS:APP3e 深入理解计算机系统_3e bomblab实验

    bomb.c /*************************************************************************** * Dr. Evil's Ins ...

  9. Benchmarking Apache Kafka: 2 Million Writes Per Second (On Three Cheap Machines)

    I wrote a blog post about how LinkedIn uses Apache Kafka as a central publish-subscribe log for inte ...

随机推荐

  1. PTA数据结构与算法题目集(中文) 7-5

    PTA数据结构与算法题目集(中文)  7-5  堆中的路径 7-5 堆中的路径 (25 分)   将一系列给定数字插入一个初始为空的小顶堆H[].随后对任意给定的下标i,打印从H[i]到根结点的路径. ...

  2. SI24R2F新一代2.4G超低功耗单发射有源RFID芯片 SI24R2E升级版智能充电安全管理方案首选

    目前全国有很多电动车因在充电时电池温度过高,而导致爆炸引起火灾的情况.作为国内RFID行业的推动者,动能世纪联合中科微向IOT应用领域推出新款大功率2.4G射频芯片,并针对电动车防盗.电动车充电桩市场 ...

  3. 如何在Vue中优雅的使用防抖节流

    1. 什么是防抖节流 防抖:防止重复点击触发事件 首先啥是抖? 抖就是一哆嗦!原本点一下,现在点了3下!不知道老铁脑子是不是很有画面感!哈哈哈哈哈哈 典型应用就是防止用户多次重复点击请求数据. 代码实 ...

  4. scrapy中使用selenium来爬取页面

    scrapy中使用selenium来爬取页面 from selenium import webdriver from scrapy.http.response.html import HtmlResp ...

  5. Win安装docker

    Windows Docker 安装 win7.win8 系统 win7.win8 等需要利用 docker toolbox 来安装,国内可以使用阿里云的镜像来下载,下载地址:http://mirror ...

  6. 支持向量机SVM推导

    样本(\(x_{i}\),\(y_{i}\))个数为\(m\): \[\{x_{1},x_{2},x_{3}...x_{m}\} \] \[\{y_{1},y_{2},y_{3}...y_{m}\} ...

  7. 数据结构和算法(Golang实现)(5)简单入门Golang-接口

    接口 在Golang世界中,有一种叫interface的东西,很是神奇. 一.数据类型 interface{} 如果你事前并不知道变量是哪种数据类型,不知道它是整数还是字符串,但是你还是想要使用它. ...

  8. 【漏洞通告】Linux Kernel 信息泄漏&权限提升漏洞(CVE-2020-8835)通告

    0x01漏洞简介: 3月31日, 选手Manfred Paul 在Pwn2Own比赛上用于演示Linux内核权限提升的漏洞被CVE收录,漏洞编号为CVE-2020-8835.此漏洞由于bpf验证系统在 ...

  9. 子域名爆破工具:OneForALL

    0x00 简介 OneForAll是一款功能强大的子域收集工具 0x01 下载地址 码云: https://gitee.com/shmilylty/OneForAll.git Github: http ...

  10. TcxLookupComboBox

    1.绑定数据源显示 cxLookupComboBox1.Properties.DropDownAutoSize:=true; //设置下拉列表为自适应宽度 cxLookupComboBox1.Prop ...