CodeForces 280B Maximum Xor Se
题目链接:http://codeforces.com/contest/280/problem/B
题目大意:
给定一个由n个数组成的一个序列,s[l..r] (1 ≤ l < r ≤ n)代表原序列中从第l个到第r个组成的子序列,对于每一个这样的序列,都有一个幸运数字,其值为序列中最大的2个数字异或的值,求所有这些幸运数字中最大的是多少。
分析:
假定所有数都可以用k位二进制位表示,不妨设所有数的第k位二进制位不全相同(全相同就可以一起去掉,对答案没影响),那么取得最优解的s[l..r]中一定有且只有一个数,其第k位二进制位为1,其余数的第k位二进制位都为0。
代码如下:
#include <bits/stdc++.h>
using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define pii pair<int,int>
#define piii pair<pair<int,int>,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} typedef long long LL;
typedef unsigned long long uLL;
const int inf = 1e9 + ;
const LL mod = 1e9 + ;
const int maxN = 1e5 + ; int n;
int s[maxN];
int ans[maxN]; int main(){
while(cin >> n) {
// highBit最高二进制差异位,比如{101100, 101101, 101011, 101010},
// 那么highBit = 000100,因为4个数有共同的高3位101,到第四位不同
int max_1 = -, min_1 = inf, highBit = ;
int ans = -;
For(i, , n) {
cin >> s[i];
highBit |= s[i];
max_1 = max(max_1, s[i]);
min_1 = min(min_1, s[i]);
} while(highBit & ~LOWBIT(highBit)) highBit &= ~LOWBIT(highBit); while(!((max_1 & highBit) ^ (min_1 & highBit))) {
max_1 &= ~highBit;
min_1 &= ~highBit;
highBit >>= ;
} For(i, , n) {
if(s[i] & highBit) {
int tmp_max = -;
For(j, i + , n) {
if(s[j] & highBit) {
i = j - ;
break;
}
tmp_max = max(tmp_max, s[j]);
ans = max(ans, tmp_max ^ s[i]);
}
}
}
rFor(i, n, ) {
if(s[i] & highBit) {
int tmp_max = -;
rFor(j, i - , ) {
if(s[j] & highBit) {
i = j + ;
break;
}
tmp_max = max(tmp_max, s[j]);
ans = max(ans, tmp_max ^ s[i]);
}
}
} cout << ans <<endl;
}
return ;
}
CodeForces 280B Maximum Xor Se的更多相关文章
- Codeforces 484B Maximum Value(高效+二分)
题目链接:Codeforces 484B Maximum Value 题目大意:给定一个序列,找到连个数ai和aj,ai%aj尽量大,而且ai≥aj 解题思路:类似于素数筛选法的方式,每次枚举aj,然 ...
- LeetCode 421. 数组中两个数的最大异或值(Maximum XOR of Two Numbers in an Array) 71
421. 数组中两个数的最大异或值 421. Maximum XOR of Two Numbers in an Array 题目描述 给定一个非空数组,数组中元素为 a0, a1, a2, - , a ...
- Codeforces C. Maximum Value(枚举二分)
题目描述: Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Maximum Xor Secondary CodeForces - 281D (单调栈)
Bike loves looking for the second maximum element in the sequence. The second maximum element in the ...
- CodeForces 276D – Little Girl and Maximum XOR 贪心
整整10个月后第二次搞这个问题才搞懂........第一次还是太随意了. 解题思路: 经过打表可得规律答案要么是0 要么是2的N次 - 1 要得到最大的XOR值,其值一定是2的N次 - 1 即在 l ...
- Codeforces Round #172 (Div. 2) D. Maximum Xor Secondary 单调栈应用
http://codeforces.com/contest/281/problem/D 要求找出一个区间,使得区间内第一大的数和第二大的数异或值最大. 首先维护一个单调递减的栈,对于每个新元素a[i] ...
- Codeforces 276D Little Girl and Maximum XOR
题意:给范围l,r选两个数亦或最大是多少. 思路:找到第一个l和r二进制下不相同的位置i,然后答案就是2^(i+1)-1,因为一个取0一个取1之后,后面的位置全部选1和全部选0,就是这样:011111 ...
- [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...
- Leetcode: Maximum XOR of Two Numbers in an Array
Given a non-empty array of numbers, a0, a1, a2, - , an-1, where 0 ≤ ai < 231. Find the maximum re ...
随机推荐
- Sql Server 2008日志满的解决办法
通过sql命令 USE ZGZY; GO --由完整模式设置为简单恢复模式 ALTER DATABASE ZGZY SET RECOVERY SIMPLE WITH NO_WAIT GO --收缩日志 ...
- 修改SublimeText3插件Emmet生成HTML中lang属性的默认值
打开Preferences → Package Settings → Emmet → Settings-User,输入如下代码并保存: { "snippets": { " ...
- 【20190305】CSS-响应式图片:srcset+sizes,picture,svg
响应式图片可以根据不同的设备屏幕大小从而选择加载不同的图片,从而节省带宽.实现响应式图片有三种方法:srcset+sizes属性.picture标签.svg 1. srcset+sizes srcse ...
- 小记 xian80 坐标转换 wgs84
转坐标这个问题是个老生常谈的话题了. 昨天遇到同事求助将 xian80的平面坐标转换到2000下. 想了一下,因为暂时还没有现成的2000的dwg数据可用,只能暂时以wgs84的为准了,然而有个问题, ...
- Mapbox浅析(快速入门Mapbox)
1.是什么? Mapbox是一个可以免费创建并定制个性化地图的网站. 2.了解一些基本东西 常见的 mapbox.js和mapbox-gl.js的异同点? 相同点: 1.都是由Mapbox公司推出的免 ...
- 【腾讯云服务器】基于centos7搭建ftp服务器(vsftpd)
该博客分为三部分设置,1.ftp服务器搭建.2.防火墙设置 3.腾讯云安全组 一.ftp服务器搭建 1.1 安装vsftpd yum install vsftpd -y 1.2 启动vsftpd服 ...
- spring基本知识
什么是spring: spring就是以IOC反转控制和AOP面向切面编程为内核,使用基本的JavaBean来完成以前由EJB完成的工作. spring框架的优点: (1)方便耦合,简化开发:spri ...
- 尝试Java,从入门到Kotlin(下)
上篇已提(tu)到(cao)Java中的各种坑.习惯了C#的各种特性和语法糖后,再转到Java感觉比较别扭.最后本着反正Java也不是很熟悉,干脆再折腾折腾其他语言的破罐子破摔的心态,逛了一圈JVM语 ...
- MongoDB的常用命令和增查改删
数据库操作 Mongodb MySQL 查询库 show databases | show dbs show databases 选中库 use databaseName use databaseNa ...
- MongoDB在Linux系统下的安装与启动
Mongodb介绍 MongoDB是一个开源文档数据库,提供高性能,高可用性和自动扩展,官方文档:https://docs.mongodb.com/manual/introduction/ Mongo ...