挂个链接

Description:

给你 \(n\) 个数 \(a_1,a_2,……,a_n\) ,让你找出一个 \(x\) ,使 \(x\) 分别异或每一个数后得到的 \(n\) 个结果的最大值最小。

Solution:

设 \(x\) 为题中所说, \(m\) 为 \(max{x^a_i}\) :

构建 \(01Trie\) ,将所有数的二进制形式存到 \(01Trie\) 上,对于第 \(k\) 位,存在两种情况:

  • 一是都是0或都是1,这样只要使 \(x\) 的第 \(k\) 位为1或0,就可以使 \(m\) 的第k位为0.根据贪心的策略,高位为0的数一定比高位为1的数小,不管低位怎样。

  • 二是这一位0和1都有,那么分开dfs,取最大值。这种情况不太好理解,具体可以见代码。

Code:

我没有建 \(Trie\) ,而是直接dfs。

\(dfs(v[],k)\) 表示 \(v\) 中的数的最小的 \(m\) ,并且已经搜到第 \(k\) 位了。(锹黑板,划重点!!!


#include<bits/stdc++.h>
using namespace std;
typedef long long ll; const ll N = 1e5+1;
ll n,ans; ll dfs(vector<ll> v,ll k)
{
vector<ll> v1,v2;
if(k<0) return 0;
for(int i=0;i<v.size();++i)
{
if((v[i]>>k)&1) v1.push_back(v[i]);
else v2.push_back(v[i]);
}
if(v1.empty()||v2.empty()) return dfs(v,k-1);//01Trie 只有一个分支
ll d1=dfs(v1,k-1),d2=dfs(v2,k-1);
return min(max(d1,d2+(1<<k)),max(d1+(1<<k),d2));//有两个分支
} int main()
{
vector<ll> v;
scanf("%lld",&n);
while(n--)
{
ll tmp;scanf("%lld",&tmp);
v.push_back(tmp);
}
printf("%lld\n",dfs(v,30));
return 0;
}

CF1285D 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. CF1285 --- Dr. Evil Underscores

    CF1285 --- Dr. Evil Underscores 题干 Today as a friendship gift, Bakry gave Badawy \(n\) integers \(a_ ...

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

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

  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. 爬虫 - Scrapy中间件

    前提:看Scrapy架构图 不管什么Middlewares,都写在middlewares.py里面. 然后在settings.py里的DOWNLOADER_MIDDLEWARES或者SPIDER_MI ...

  2. centos openoffice 的启动

    https://blog.csdn.net/resolute123/article/details/77304973

  3. TensorFlow:谷歌图像识别网络inception-v3下载与查看结构

    学习博客: # https://www.cnblogs.com/felixwang2/p/9190731.html # https://www.cnblogs.com/felixwang2/p/919 ...

  4. Flutter Container 组件、Text 组件详解

    Text 组件 textAlign     文本对齐方式(center 居中,left 左对齐,right 右对齐,justfy 两端对齐) textDirection   文本方向(ltr 从左至右 ...

  5. Python使用Tensorflow出现错误: UserWarning: The default mode, 'constant'

    Python使用Tensorflow出现错误: UserWarning: The default mode, 'constant', will be changed to 'reflect' in s ...

  6. MyBatis-Plus学习笔记(2):代码生成器

    AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity.Mapper.Mapper XML.Service.Control ...

  7. Tornado项目简单创建

    Tornado是使用Python编写的一个强大的.可扩展的Web服务器.它在处理严峻的网络流量时表现得足够强健,但却在创建和编写时有着足够的轻量级,并能够被用在大量的应用和工具中. tornado技术 ...

  8. BeanUtils学习笔记

    一. 简介 BeanUtils提供对Java反射和自省API的包装.其主要目的是利用反射机制对JavaBean的属性进行简化操作处理.一个JavaBean通常包含了大量的属性,很多情况下,对JavaB ...

  9. IPv4地址被用光,IPv6将接手

    截止2019年11月26号,全球所有43亿个IPv4地址已全部分配完毕,这一情况也宣告着IPv6时代的正式来临.IPv6和5G一样是关系到国家安全和战略发展的重大事情. IPv6简单来说,就是一个互联 ...

  10. 卷积神经网络(CNN)_相关知识

    斯坦福公开课 CS231n Convolutional Neural Networks for Visual Recognition : http://cs231n.stanford.edu/syll ...