CF1285 --- Dr. Evil Underscores
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
orconquer 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\).又因为我们要进行贪心寻找---从高位往地位找,这样才能保证我们在某数组为空时舍弃另一数组的正确性。在都不为空时进行分治。总结如下:
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的更多相关文章
- DFS-B - Dr. Evil Underscores
B - Dr. Evil Underscores Today, as a friendship gift, Bakry gave Badawy nn integers a1,a2,…,ana1,a2, ...
- codeforces 1285D. Dr. Evil Underscores(字典树)
链接:https://codeforces.com/problemset/problem/1285/D 题意:给n个数a1,a2,a3.....an,找到一个数X,使得X 异或所有的ai ,得到的ma ...
- CF1285D Dr. Evil Underscores
挂个链接 Description: 给你 \(n\) 个数 \(a_1,a_2,--,a_n\) ,让你找出一个 \(x\) ,使 \(x\) 分别异或每一个数后得到的 \(n\) 个结果的最大值最小 ...
- C - Dr. Evil Underscores CodeForces - 1285D 二进制
题目大意:n个数,任意整数x对这n个数取异或值,然后使最大值最小. 思路:数据范围最大为pow(2,30);所以考虑二进制的话,最多有30位.对于某一位d,然后考虑数组v中每一个元素的d为是0还是1, ...
- codeforces每日一题1-10
目录: 1.1093D. Beautiful Graph(DFS染色) 2.514C - Watto and Mechanism(Tire) 3.69E.Subsegments(STL) 4.25C. ...
- Codeforces Round #613 (Div. 2) A-E简要题解
contest链接:https://codeforces.com/contest/1285 A. Mezo Playing Zoma 签到 #include<iostream> #incl ...
- 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 ...
- CS:APP3e 深入理解计算机系统_3e bomblab实验
bomb.c /*************************************************************************** * Dr. Evil's Ins ...
- 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 ...
随机推荐
- 【Java技术系列】爱情36技之暗送秋波的技术
1. 这篇文章想分享已经很久了,苦于皱巴巴的技术比较生涩难懂,迟迟没有找到好的分享方式,今天结合爱情中暗送秋波的故事的形式,尝试分享一下. 以后如果再有人问你们:能否在加载类的时候,对字节码进行修改? ...
- Springboot项目中 前端展示本地图片
Springboot项目中 前端展示本地图片 本文使用的是Springboot官方推荐的thymeleaf(一种页面模板技术) 首先在pom文件加依赖 <dependency> <g ...
- 打开scratch后蓝屏怎么办
1.试试开机,百出完电脑品牌后,按F8,安全模式,光标选定:最后一次正确配置,回车,回车,按下去,[度关键一步]2.再不行,问进安全模式,回车,到桌面后,用杀毒软件腾讯电脑管家,全盘杀毒,“隔离区”的 ...
- 家庭记账本app进度之ui相关概念控制ui界面与布局管理
ui就是用户界面设计的意思. 首先是view,view相当于窗户上的玻璃. 1.android:id属性.android:id="@+id/user".他的id是user前面的@+ ...
- PHP如何配置session存储在redis
当网站用户量增多的时候,正常的session存取就会出现有点慢的问题,如果提高速度呢. 我们可以使用redis去保存session的会话信息. PHP的会话默认是以文件的形式存在的,可以配置到NoSQ ...
- .bundle文件如何安装
1. sudo chmod +x X.bundle 2. sudo X.bundle
- Pytest系列(17)- pytest-xdist分布式测试的原理和流程
pytest-xdist分布式测试的原理 前言 xdist的分布式类似于一主多从的结构,master机负责下发命令,控制slave机:slave机根据master机的命令执行特定测试任务 在xdist ...
- spark模型error java.lang.IllegalArgumentException: Row length is 0
failure: Lost task 18.3 in stage 17.0 (TID 59784,XXXXX, executor 19): java.lang.IllegalArgumentExcep ...
- python3(三)enc
# ASCII编码和Unicode编码的区别:ASCII编码是1个字节,而Unicode编码通常是2个字节. # Unicode把所有语言都统一到一套编码里,这样就不会再有乱码问题了. # 新的问题又 ...
- Chrome浏览器架构
通用浏览器架构 它可以是一个具有许多不同线程的进程,也可以是具有几个通过IPC进行通信的多个线程的进程. 一个具有许多不同线程的进程 通过IPC进行通信的多个线程的进程 注意 这些不同的体系结构是实现 ...