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