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 ...
随机推荐
- STM32F103C8T6最小系统开发板原理图
1.
- ASP.NET CORE WEBAPI文件下载
ASP.NET CORE WEBAPI文件下载 最近要使用ASP.NET CORE WEBAPI用来下载文件,使用的.NET CORE 3.1.考虑如下场景: 文件是程序生成的. 文件应该能兼容各种格 ...
- 【mysql】mysql优化
一,表设计 1.1. E-R(entity relation)实体关系图 长方形 实体 表 椭圆形 属性 字段 菱形 关系 一对一 多对一 属于 多对多 1.2. 三范式标准 原子性 个人信息 省市县 ...
- Vulnhub DC-4靶机渗透
信息搜集 nmap -sP 192.168.146.0/24 #扫网段看看存活的主机,找到靶机 nmap -sS -Pn -A 192.168.146.141 可以看到开放了22和80端口,那么就可以 ...
- Mysql 临时表+视图
原文地址:http://www.cnblogs.com/mrdz/p/6195878.html 学习内容: 临时表和视图的基本操作... 临时表与视图的使用范围... 1.临时表 临时表:临时表, ...
- 28.2 api-- System (gc、arraycopy、exit)
/* * System:包含一些有用的类字段和方法.它不能被实例化 * static void arraycopy(Object src, int srcPos, Object dest, int d ...
- template_homepage
homepage用到的方法 使用 {% for ... in ...%} 加 {% endfor %} 实现循环结构 举例: {% for post in posts %} <p style ...
- hadoop(十)hdfs上传删除文件(完全分布式七)|12
集群测试 上传小文件到集群,随便选择一个小文件上传到hdfs的根目录 [shaozhiqi@hadoop102 hadoop-3.1.2]$ bin/hdfs dfs -put wcinput/wc. ...
- C语言局部数组大小与内存的栈的关系
今天有个同学问了一个问题,我居然答不上来,为什么不能开局部整型二维数组[1000][1000]?但是在数组前面加上一个static就可以了? windows下栈的大小(不是数据结构里面的栈)是2MB, ...
- softmax回归推导
向量\(y\)(为one-hot编码,只有一个值为1,其他的值为0)真实类别标签(维度为\(m\),表示有\(m\)类别): \[y=\begin{bmatrix}y_1\\ y_2\\ ...\\y ...