题目链接: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. 委托(2).net 1.x中的委托

    上一篇已经演示了使用委托实现一个多语言问候的程序,这一篇文章来总结一下在.net 1.x中委托的使用方法. 既然委托是一个类型(class),那么它就要经历像类一个先声明,然后new一个对象,最后调用 ...

  2. ES10特性详解

    摘要: 最新的JS特性. ES10 还只是一个草案.但是除了 Object.fromEntries 之外,Chrome 的大多数功能都已经实现了,为什么不早点开始探索呢?当所有浏览器都开始支持它时,你 ...

  3. 前端导出excel数据-jsonToExcel

    咳咳,好久没有写博了... 在工作中遇到了纯前端,将数据导出为excel文件.正文开始: 第一步 安装依赖: npm i xlsx 第二步 写导出函数: import XLSX from 'xlsx' ...

  4. 将Dynamics 365中的用户及其角色、角色导出到Excel中

    关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复240或者20161204可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong. ...

  5. Android Camera2 预览功能实现

    1. 概述 最近在做一些关于人脸识别的项目,需要用到 Android 相机的预览功能.网上查阅相关资料后,发现 Android 5.0 及以后的版本中,原有的 Camera API 已经被 Camer ...

  6. android 开发之 ListView 与Adapter 应用实践

    在开发android中,ListView 的应用显得非常频繁,只要需要显示列表展示的应用,可以说是必不可少,下面是记录开发中应用到ListView与Adapter 使用的实例: ListView 所在 ...

  7. (二)版本控制管理器之CVS(上)

    在前一篇<(一)版本控制管理器之发展史>的介绍中,有提到古典时期的CVS,那什么是CVS?CVS特点是什么?怎么个用法?等一系列的问题,虽然这个版本控制管理器早已过时,但大家了解下也不妨, ...

  8. MySQL 使用Navicat连接MySQL8出现1251错误

    安装了MySQL8.x.x后使用Navicat连接总是出现1251错误,故在此记录一下解决方法. 错误提示 1251-Client does not support authentication pr ...

  9. 基本的CRUD操作

    导入包---实体类------数据库连接----数据库操作----service层数据操作----网页对service层可视化实现 model package com.ij34.model; publ ...

  10. 使用Linq的泛型功能

    泛型数据访问类: 业务抽象类使用数据访问类: 业务类继承业务抽象类: 使用业务类: