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. VirtualBox 安装 Arch Linux 并配置桌面环境

    最近无聊,就找来 Arch Linux 来玩一玩,去 archlinux wiki上看了一下教程.以下是操作过程. 1. 下载镜像,下载地址; 2. 启动 Archlinux 并选择 Boot Arc ...

  2. keras与卷积神经网络(CNN)实现识别minist手写数字

    在本篇博文当中,笔者采用了卷积神经网络来对手写数字进行识别,采用的神经网络的结构是:输入图片——卷积层——池化层——卷积层——池化层——卷积层——池化层——Flatten层——全连接层(64个神经元) ...

  3. 汇编刷题:统计2000H开始的正负数的个数

    DATA SEGMENT ORG 2000H INFO DB 1,2,3,4,5,70H,71H,72H,80H,92H N_NUMS DB 00H P_NUMS DB 00H DATA ENDS C ...

  4. MODIS系列之NDVI(MOD13Q1)二:modis数据相关信息

    1.MODIS数据的特点 (1)全球免费:NASA对MODIS数据实行全球免费接收的政策(TERRA卫星除MODIS外的其他传感器获取的数据均采取公开有偿接收和有偿使用的政策),这样的数据接收和使用政 ...

  5. 听说你想要部署 Octopress?满足你

    Octopress 是一个面向开发者的博客系统,广受程序员的喜爱.既然大家有需求,那么 Octopress 也要安排上~ 云开发(CloudBase)是一款云端一体化的产品方案 ,采用 serverl ...

  6. 《面试经典系列》- SpringMVC原理及工作流程

    前言 SpringMVC 作为 MVC 的开源框架,现在依旧是不少项目使用的重点框架.SpringMVC = Struts2 + Spring,SpringMVC就相当于 Struts2 + Spri ...

  7. view事件分发源码理解

    有些困难无法逃避,没办法,那就只有去解决它.view事件分发对我而言是一块很难啃的骨头,看了<安卓开发艺术探索>关于这个知识点的讲解,看了好几遍,始终不懂,最终通过调试分析结果,看博客,再 ...

  8. for循环,for…in循环,forEach循环的区别

    for循环,for…in循环,forEach循环的区别for循环通关for循环,生成所有的索引下标for(var i = 0 ; i <= arr.length-1 ; i++){ 程序内容 } ...

  9. Windows下如何将一个程序设为开机自启

    1.放在  开始-启动(C:\Users\Qi\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup)2.修改注册表[HKEY_L ...

  10. Python 小技之实现的鲜花盛宴,你准备好了吗?

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:派森酱 PS:如有需要Python学习资料的小伙伴可以加点击下方链接 ...