Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >Dr. Evil Underscores

Dr. Evil Underscores

作者头像
某些人
发布于 2020-04-09 04:06:23
发布于 2020-04-09 04:06:23
46900
代码可运行
举报
文章被收录于专栏:一Wa哇一天一Wa哇一天
运行总次数:0
代码可运行

题目:Dr. Evil Underscores

D. Dr. Evil Underscores

time limit per test:1 second memory limit per test:256 megabytes inputstandard input outputstandard output

Today, as a friendship gift, Bakry gave Badawy n integers a1,a2,…,an and challenged him to choose an integer X such that the value max1≤i≤n(ai⊕X) is minimum possible, where ⊕ denotes the bitwise XOR operation.

As always, Badawy is too lazy, so you decided to help him and find the minimum possible value of max1≤i≤n(ai⊕X).

Input The first line contains integer n (1≤n≤105). The second line contains n integers a1,a2,…,an (0≤ai≤230−1).

Output Print one integer — the minimum possible value of max1≤i≤n(ai⊕X).

input

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
3
1 2 3

output

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
2

input

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
2
1 5

output

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
4

Note In the first sample, we can choose X=3. In the second sample, we can choose X=5.

题目大意

给你n个数,让你寻找一个X,使X异或这n个数的最大值尽可能的要小

解题思路

第一题写这个题感觉还挺有意思的,仔细想了想查了查博客终于明白了,既然是位运算那肯定和二进制有关,把每个数据都按二进制去思考,如果这些数某个二进制位置上的数既有0又有1,那么不论取值X的二进制位置上对应的位置上是0还是1,这个位置上异或后的值一定是1!所以,我们每次统计一下,如果这个位置既有0又有1,那么这个位置异或后肯定是1,如果这个位置上的数据相同,那么我们就不用管它,让这个位置上的值为0,就好了最大值尽可能的小;然后我们这样每次按位处理,最多30次就可以处理完!

代码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#include<bits/stdc++.h>
using namespace std;
int n;
int solve(vector<int> ve,int x)
{
    if(x==-1) return 0;
    if(ve.size()==0) return 0;//递归到头了,没有数据了;直接返回就可以 
    vector<int> l,r;//区分给位置的数值  
    for(int i:ve) 
    {
        if(i&(1<<x)) l.push_back(i);//如果是 1 就放进 l 里面 
        else r.push_back(i);//0就放进 r里面; 
    }
    if(l.size()==0) return solve(r,x-1);//左边为空说明这个位置都是 1 ,直接处理右边 
    if(r.size()==0) return solve(l,x-1);//右边为空说明这个位置都是 0 ,直接处理左边 
    return min(solve(r,x-1),solve(l,x-1))+(1<<x);//因为是要最小值,所以让最小的加上  
}                                    //二进制该位置唯一的十进制数(类似进制转换)        

int main()
{
    cin>>n;
    vector<int>v(n);
    for(int i=0;i<n;i++)
    {
        cin>>v[i];
    }
    cout<<solve(v,29)<<'\n';
    return 0;
} 

我的博客即将同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=2heqlgm91i04w

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-04-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Codeforces Round #633 (Div. 2)D Edge Weight Assignment(构造、树的权值异或)
You have unweighted tree of nn vertices. You have to assign a positive weight to each edge so that the following condition would hold:
glm233
2020/09/28
3130
Codeforces Round #633 (Div. 2)D Edge Weight Assignment(构造、树的权值异或)
CodeForces - 1245F Daniel and Spring Cleaning (数位DP)
While doing some spring cleaning, Daniel found an old calculator that he loves so much. However, it seems like it is broken. When he tries to compute 1+31+3 using the calculator, he gets 22 instead of 44. But when he tries computing 1+41+4, he gets the correct answer, 55. PuFzzled by this mystery, he opened up his calculator and found the answer to the riddle: the full adders became half adders!
风骨散人Chiam
2020/10/28
4560
leetcode-137-Single Number II-第一种解法
题目描述: Given an array of integers, every element appears three times except for one, which appears ex
chenjx85
2018/05/21
1.4K0
Educational Codeforces Round 81 (Rated for Div. 2)
B. Infinite Prefixes 无限前缀 You are given string ? of length ? consisting of 0-s and 1-s. You build an
ACM算法日常
2020/02/17
3730
Educational Codeforces Round 81 (Rated for Div. 2)
HDU3949 XOR(线性基第k小)
XOR is a kind of bit operator, we define that as follow: for two binary base number A and B, let C=A XOR B, then for each bit of C, we can get its value by check the digit of corresponding position in A and B. And for each digit, 1 XOR 1 = 0, 1 XOR 0 = 1, 0 XOR 1 = 1, 0 XOR 0 = 0. And we simply write this operator as ^, like 3 ^ 1 = 2,4 ^ 3 = 7. XOR is an amazing operator and this is a question about XOR. We can choose several numbers and do XOR operatorion to them one by one, then we get another number. For example, if we choose 2,3 and 4, we can get 2^3^4=5. Now, you are given N numbers, and you can choose some of them(even a single number) to do XOR on them, and you can get many different numbers. Now I want you tell me which number is the K-th smallest number among them.
attack
2018/08/01
3300
hdu 4057 AC自己主动机+状态压缩dp
大家好,又见面了,我是全栈君。 http://acm.hdu.edu.cn/showproblem.php?pid=4057 Problem Description Dr. X i
全栈程序员站长
2022/01/24
1730
AcWing 第69场周赛
4615. 相遇问题 ---- 原题链接 题目大意: 求一维数轴上 x 和 y 分别以速度 a,b 相向而行时,相遇所需时间是否为整数。 ---- 思想: 签到题。 输出判断 a + b 是否可以整除 y - x 即可。 ---- 代码: #include <bits/stdc++.h> using namespace std; typedef long long LL; void sovle(){ LL x, y, a, b; cin >> x >> y >> a >> b; if
浪漫主义狗
2022/10/28
2470
算法:位运算
•反码:正数的反码就是原码,负数的反码是符号位不变,其余位取反(对应正数按位取反)
用户3578099
2022/04/18
1.1K0
算法:位运算
Codeforces Round #336 (Div. 2)【A.思维,暴力,B.字符串,暴搜,前缀和,C.暴力,D,区间dp,E,字符串,数学】
A. Saitama Destroys Hotel time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output Saitama accidentally destroyed a hotel again. To repay the hotel company, Genos has volunteered to operate an elevator in
Angel_Kitty
2018/04/09
9720
Codeforces Round #408 (Div. 2)(A.水,B,模拟)
A. Buying A House time limit per test:2 seconds memory limit per test:256 megabytes input:standard i
Angel_Kitty
2018/04/08
6800
Codeforces Round #408 (Div. 2)(A.水,B,模拟)
HDU 1394 Minimum Inversion Number (数据结构-段树)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9514 Accepted Submission(s): 5860
全栈程序员站长
2022/07/06
1900
BZOJ3668: [Noi2014]起床困难综合症(贪心 二进制)
21 世纪,许多人得了一种奇怪的病:起床困难综合症,其临床表现为:起床难,起床后精神不佳。作为一名青春阳光好少年,atm 一直坚持与起床困难综合症作斗争。通过研究相关文献,他找到了该病的发病原因:在深邃的太平洋海底中,出现了一条名为 drd 的巨龙,它掌握着睡眠之精髓,能随意延长大家的睡眠时间。正是由于 drd 的活动,起床困难综合症愈演愈烈,以惊人的速度在世界上传播。为了彻底消灭这种病,atm 决定前往海底,消灭这条恶龙。历经千辛万苦,atm 终于来到了 drd 所在的地方,准备与其展开艰苦卓绝的战斗。drd 有着十分特殊的技能,他的防御战线能够使用一定的运算来改变他受到的伤害。具体说来,drd 的防御战线由 n扇防御门组成。每扇防御门包括一个运算op和一个参数t,其中运算一定是OR,XOR,AND中的一种,参数则一定为非负整数。如果还未通过防御门时攻击力为x,则其通过这扇防御门后攻击力将变为x op t。最终drd 受到的伤害为对方初始攻击力x依次经过所有n扇防御门后转变得到的攻击力。由于atm水平有限,他的初始攻击力只能为0到m之间的一个整数(即他的初始攻击力只能在0,1,...,m中任选,但在通过防御门之后的攻击力不受 m的限制)。为了节省体力,他希望通过选择合适的初始攻击力使得他的攻击能让 drd 受到最大的伤害,请你帮他计算一下,他的一次攻击最多能使 drd 受到多少伤害。
attack
2018/07/27
2700
BZOJ 1411&&Vijos 1544 : [ZJOI2009]硬币游戏【递推,快速幂】
1411: [ZJOI2009]硬币游戏 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 897  Solved: 394 [Submit][Status][Discuss] Description Orez很喜欢玩游戏,他最近发明了一款硬币游戏。他在桌子的边缘上划分出2*n个位置并按顺时针把它们标号为1,2,……,2n,然后把n个硬币放在标号为奇数的位置上。接下来每次按如下操作:在任意两个硬币之间放上一个硬币,然后将原来的硬币拿走;所放硬币的正反面由它两边
Angel_Kitty
2018/04/09
5830
E. Permutation Separation
E. Permutation Separation time limit per test:2 seconds memory limit per test:256 megabytes inputstandard input outputstandard output
某些人
2020/04/09
3610
Little Girl and Maximum XOR(区间最大异或值--技巧)-------------C语言——菜鸟级
A little girl loves problems on bitwise operations very much. Here’s one of them. You are given two integers l and r. Let’s consider the values of for all pairs of integers a and b (l ≤ a ≤ b ≤ r). Your task is to find the maximum value among all considered ones. Expression means applying bitwise excluding or operation to integers x and y. The given operation exists in all modern programming languages, for example, in languages C++ and Java it is represented as “^”, in Pascal — as «xor». Input The single line contains space-separated integers l and r (1 ≤ l ≤ r ≤ 1018). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Output In a single line print a single integer — the maximum value of for all pairs of integers a, b (l ≤ a ≤ b ≤ r). Example Input 1 2 Output 3 Input 8 16 Output 31 Input 1 1 Output 0
Fivecc
2022/11/21
3140
D. Minimax Problem
time limit per test:5 seconds memory limit per test:512 megabytes inputstandard input outputstandard output
某些人
2020/04/09
2960
【leetcode刷题】T33-只出现一次的数字
Given a non-empty array of integers, every element appears twice except for one. Find that single one.
木又AI帮
2019/07/17
3650
Code Forces 650 C Table Compression(并查集)
C. Table Compression time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard output Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorithms and many others.
ShenduCC
2018/04/26
6590
【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C
饶文津
2020/06/02
3930
Codeforces Round #629 (Div. 3) F. Make k Equal (技巧暴力,类前缀和,思维,数学)
You are given the array aa consisting of nn elements and the integer k≤nk≤n.
glm233
2020/09/28
4640
相关推荐
Codeforces Round #633 (Div. 2)D Edge Weight Assignment(构造、树的权值异或)
更多 >
加入讨论
的问答专区 >
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
    本文部分代码块支持一键运行,欢迎体验
    本文部分代码块支持一键运行,欢迎体验