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}\)
bitmasksdivide and conquerdfs and similardp*1800
思路分析
这题就是选取一个\(X\)值,让序列\(a_1,a_2, \cdots ,a_n\)异或他之后的最大值最小。实际上,这个题目是有两个关键词。
- 对一个序列进行异或,或者区间异或 \(\rightarrow\) \(\mathcal{Trie\; Tree}\)(字典树)
- 最大值最小问题 \(\rightarrow\)
binary searchorconquer 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 ...
随机推荐
- Oracle创建函数例子
编写一个函数计算学生某一门课程在班级内的排名. 表结构如下: create or replace function fun_score_rank( p_in_stuid in number,--学号 ...
- javascript中常见的表单验证项
1.不能超过20个字符 <body> <form name=a onsubmit="return test()"> <textarea name=&q ...
- autojs,autojs 发送http请求,autojs 解析json数据
如题,我这个就直接上代码吧 (function () { let request = http.request; // 覆盖http关键函数request,其他http返回最终会调用这个函数 http ...
- 三通道低功耗ASK低频唤醒接收器PAN3501完全替代AS3933/GC3933
低频唤醒接收器PAN3501软硬件兼容AS3933/GC3933,且新增了寄存器功能,可直接替换,供应稳定,高性价比. 产品介绍: PAN3501是一款最多三个通道接收的低功耗ASK接收机,可用 ...
- 多转一ETH(ERC20代币汇集)
1.下载表格模板 2.导入小号地址 NO1.地址整理 NO2.地址导入 NO3.导入完成 3.查询地址余额1.下图是汇集ETH的操作图片 2.下图是汇集ERC20代币的操作图片 注 ...
- Vulnhub DC-3靶机渗透
修改错误配置 打开了ova文件会发现,怎么也找不到DC-3的ip地址,估计是网卡出了问题. 那么就先配置下网卡. 进入上面这个页面之前按e. 将这里的ro 替换为 rw signie init=/bi ...
- 使用 python 创建&更改 word 文档
使用 python 修改 word 文档 说明:这个需求是老师想要一个自动识别 word 文档中指定位置的分数,并填入相应表格. 使用库 python-docx 的官方文档地址是:python-doc ...
- Python设计模式(2)-策略模式
# 策略模式和简单工厂模式相比,少了使用switch case 做判断,然后去实例化相应的 # 对象,比简单工厂模式更灵活. 它们代码的区别就在于此处使用了抽象类代替工厂类 # coding=utf- ...
- cmd批处理转义字符%的详细解释
cmd批处理转义字符%的详细解释 在命令行中使用for时不需要双%,这源于命令解释器对命令行与批处理的处理方式不同. 1.%是个ESCAPE字符,通常将之译为转义字符,但也有更形象的译名脱逸字符.逃逸 ...
- IDEA默认KeyMap映射快捷键
编辑 快捷键 描述 Ctrl + 空格 基础代码补全(任意类.方法.变量的名字) Ctrl + Shift + 空格 智能代码补全(过滤期望类型的方法和变量列表) Ctrl + Shift + 回车 ...