题目链接: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。

用第k位二进制位的值来表示s数组中的数,大致可表示为:000100000111000110,那么取得最优解的s[l..r]一定是从其中某个1开始,向左或者向右包含几个数值为0的数,只要全部遍历一遍即可,时间复杂度为O(n)。

代码如下:

 #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的更多相关文章

  1. Codeforces 484B Maximum Value(高效+二分)

    题目链接:Codeforces 484B Maximum Value 题目大意:给定一个序列,找到连个数ai和aj,ai%aj尽量大,而且ai≥aj 解题思路:类似于素数筛选法的方式,每次枚举aj,然 ...

  2. 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 ...

  3. Codeforces C. Maximum Value(枚举二分)

    题目描述: Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standard ...

  4. Maximum Xor Secondary CodeForces - 281D (单调栈)

    Bike loves looking for the second maximum element in the sequence. The second maximum element in the ...

  5. CodeForces 276D – Little Girl and Maximum XOR 贪心

    整整10个月后第二次搞这个问题才搞懂........第一次还是太随意了. 解题思路: 经过打表可得规律答案要么是0 要么是2的N次 - 1 要得到最大的XOR值,其值一定是2的N次 - 1 即在 l ...

  6. Codeforces Round #172 (Div. 2) D. Maximum Xor Secondary 单调栈应用

    http://codeforces.com/contest/281/problem/D 要求找出一个区间,使得区间内第一大的数和第二大的数异或值最大. 首先维护一个单调递减的栈,对于每个新元素a[i] ...

  7. Codeforces 276D Little Girl and Maximum XOR

    题意:给范围l,r选两个数亦或最大是多少. 思路:找到第一个l和r二进制下不相同的位置i,然后答案就是2^(i+1)-1,因为一个取0一个取1之后,后面的位置全部选1和全部选0,就是这样:011111 ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. Springcloud 的Eureka和ZooKeeper比较

    关于CAP理论,可以去看看阮一峰的文章[http://www.ruanyifeng.com/blog/2018/07/cap.html] C(一致性)A(可用性)P(分区容错性) ZooKeeper: ...

  2. DOM编程以及domReady加载的几种方式

    1,关于DOM编程       DOM编程主要是对dom树节点进行操作,所以你必须掌握基本的节点类型,如何去获取节点名字以及值(这些相关知识你可以去网上查,这里推荐一个慕课学习网站->https ...

  3. ES6 Module export与import复合使用

    export与import复合使用 基本语法 export {...} from '文件'; 等价于 import {...} from "文件": export {...} 先加 ...

  4. jsp基础语言-jsp代码段

    jsp代码段:是放在<% %>标记之间符合java语言规范的代码片段 格式:<% 代码段 %>  代码段中可以包含用于jsp变量和方法的声明.显示表达式.HTML以及调用Jav ...

  5. Linux驱动学习1.hello world;

    最近项目需要使用Linux系统开发,借此机会学习一下Linux驱动开发 hello word代码hello.c #include <linux/module.h> #include < ...

  6. ccflow表机构与运行机制(二次开发必看)

    驰骋工作流引擎,工作流程管理系统,表结构与运行机制. ------------------------------------------------------- 前言: 1, ccflow 有自动 ...

  7. SQL查询获得指定格式内容

    Oracle中通过修改SQL语句,达到将查询的内容拼接为指定的字符串格式 eg: select '<ta:datagridItem id="' || lower(column_name ...

  8. 010 Editor v8.0.1(32 - bit) 算法逆向分析、注册机编写

    010 Editor 的逆向分析整体算下来还是比较简单的,将程序拖入OD,通过字符串搜索定位到核心代码,经过分析,主要是如下图所示的两个关键函数,返回正确的值,才算是注册成功. 00409C9B 这个 ...

  9. centos7下 svn的配置

    安装svn yum install subversion 查看安装版本 svnserve --version 创建svn版本库目录 mkdir -p /root/svn/test 创建svn版本库 s ...

  10. Python开发【第四篇】函数

    函数的作用 函数可以让编程逻辑结构化以及模块化 无论是C.C++,Java还是Python,函数是必不可少的知识点,也是很重要的知识点,函数是完成一个功能的代码块,使用函数可以使逻辑结构变得更加清晰以 ...