http://codeforces.com/contest/281/problem/D

要求找出一个区间,使得区间内第一大的数和第二大的数异或值最大。

首先维护一个单调递减的栈,对于每个新元素a[i]。要么直接插入后面,如果它插入栈内的某个元素的话。就是说有数字弹出来了,这个时候这个数字和a[i]组成的就是区间第一、第二大,9 8 5 4 3 2 1 7这样,最后那个7,弹出1,证明[7,8]这个区间第一、第二大的数字分别是1、和7,其他类似。

还有一个地方要注意的是: 要特别处理那些不能弹出的数字,就是9和8这样,不能被其他数字弹出,他们组成的区间没办法计算。解决办法就是每次都把栈顶元素和第二顶元素算一次,更新答案。这么才能不重不漏。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 1e5 + ;
LL a[maxn];
int stack[maxn];
void work ()
{
int n;
scanf ("%d", &n);
LL mx = -inf, sec = -inf;
for (int i = ; i <= n; ++i) {
scanf ("%I64d", &a[i]);
mx = max (a[i], mx);
}
for (int i = ; i <= n; ++i) {
if (a[i] > sec && a[i] != mx) {
sec = a[i];
}
}
LL ans = mx ^ sec;
int top = ;
for (int i = ; i <= n; ++i) {
while (top >= && a[i] > a[stack[top]]) {
ans = max (ans, a[i] ^ a[stack[top]]);
top--;
}
++top;
stack[top] = i;
if (top >= ) {
ans = max (ans, a[stack[top]] ^ a[stack[top - ]]);
}
}
printf ("%I64d\n", ans);
return ;
} int main ()
{
#ifdef local
freopen("data.txt","r",stdin);
#endif
work ();
return ;
}

Codeforces Round #172 (Div. 2) D. Maximum Xor Secondary 单调栈应用的更多相关文章

  1. Codeforces Round #305 (Div. 1) B. Mike and Feet 单调栈

    B. Mike and Feet Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/547/pro ...

  2. Codeforces Round #305 (Div. 2) D. Mike and Feet 单调栈

    D. Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  3. Codeforces Round #344 (Div. 2) 631 C. Report (单调栈)

    C. Report time limit per test2 seconds memory limit per test256 megabytes inputstandard input output ...

  4. Codeforces Round #622 (Div. 2) C2 - Skyscrapers (hard version) 单调栈

    从左往右扫,找到比第i个小的第一个数字,l[i] = l[last] + (i - last) * m[i],用单调栈O(n)维护这个过程,再从右往左扫,同理可以算出r数组,注意一下long long ...

  5. Codeforces Round #172 (Div. 2)

    A. Word Capitalization 模拟. B. Nearest Fraction 枚举. C. Rectangle Puzzle 求出两个矩形的点,套简单多边形的面积交板子. D. Max ...

  6. Codeforces Round #172 (Div. 1 + Div. 2)

    A. Word Capitalization 模拟. B. Nearest Fraction 枚举. C. Rectangle Puzzle 求出两个矩形的点,套简单多边形的面积交板子. D. Max ...

  7. Codeforces Round #221 (Div. 1) B. Maximum Submatrix 2 dp排序

    B. Maximum Submatrix 2 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...

  8. Codeforces Round #276 (Div. 1) B. Maximum Value 筛倍数

    B. Maximum Value Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/484/prob ...

  9. Codeforces Round #508 (Div. 2) E. Maximum Matching(欧拉路径)

     E. Maximum Matching 题目链接:https://codeforces.com/contest/1038/problem/E 题意: 给出n个项链,每条项链左边和右边都有一种颜色(范 ...

随机推荐

  1. poj 1517 u Calculate e(精度控制+水题)

    一.Description A simple mathematical formula for e is e=Σ0<=i<=n1/i! where n is allowed to go t ...

  2. HDOJ2553(2N皇后问题)

    N皇后问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  3. Python selenium 三种等待方法

    1. 强制等待 sleep(xx) 是最简单粗暴的一种办法,不管你浏览器是否加载完了,程序都得等待3秒,3秒一到,继续执行下面的代码,作为调试很有用,不建议总用这种等待方式,严重影响程序执行速度. 代 ...

  4. VisualGDB系列3:安装VisualGDB

    根据VisualGDB官网(https://visualgdb.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指正. 1 系统需求 系统需求如下: Micro ...

  5. IIS备份和还原

    当我们电脑系统有大量的站点和虚拟目录的时候,电脑因为种种原因需要重做系统,那么重装系统后这些站点我们是否只能一个一个的添加,如果有成百上千个站点呢,任务量可想而知,本文将介绍如何备份和还原window ...

  6. openssh for windows

  7. Devexpress GridControl

    1.隐藏“Drag a column header here to group by that column”如下: 选择gridview->属性 选择OptionView->ShowGr ...

  8. idea崩溃导致的svn插件丢失问题, maven dependencies视图丢失问题

    Idea丢失Svn解决办法 今天打开Idea,习惯用ctrl+t来更新svn,杯具出现了,快捷键失效了,我觉得可能是其他的什么软件占用了这个快捷键,于是重启了一下,发现还是不行,svn信息怎么没了,c ...

  9. Linux性能测试工具-UnixBench--安装以及结果分析

    参考链接:http://blog.csdn.net/gatieme/article/details/50912910 依赖包安装:      yum install -y SDL-devel mesa ...

  10. Windows上python + selenium + Firefox浏览器的环境配置

    1.python安装 我的电脑是32位的,安装了Python 3.5.4版本其它安装版本 2.python环境变量配置 将”C:\Program Files\Python35",”C:\Pr ...