Maximum Xor Secondary(单调栈好题)
Maximum Xor Secondary
CodeForces - 280B
Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1, x2, ..., xk (k > 1) is such maximum element xj, that the following inequality holds: .The lucky number of the sequence of distinct positive integers x1, x2, ..., xk (k > 1)is the number that is equal to the bitwise excluding OR of the maximum element of the sequence and the second maximum element of the sequence.
You've got a sequence of distinct positive integers s1, s2, ..., sn (n > 1). Let's denote sequence sl, sl + 1, ..., sr as s[l..r] (1 ≤ l < r ≤ n). Your task is to find the maximum number among all lucky numbers of sequences s[l..r].
Note that as all numbers in sequence s are distinct, all the given definitions make sence.
Input
The first line contains integer n (1 < n ≤ 105). The second line contains n distinct integers s1, s2, ..., sn (1 ≤ si ≤ 109).
Output
Print a single integer — the maximum lucky number among all lucky numbers of sequences s[l..r].
Examples
55 2 1 4 3
7
59 8 3 5 7
15
Note
For the first sample you can choose s[4..5] = {4, 3} and its lucky number is (4 xor 3) = 7. You can also choose s[1..2].
For the second sample you must choose s[2..5] = {8, 3, 5, 7}.
题意:给你一串数,问你这一串数的任意子串中的最大值和次大值的异或最大值为多少。
题解:
因为一个数可以是很多个区间的最大值,一个数也可以是很多个区间的次大值,所以我们可以以数为研究对象而非一个个区间。
第一种做法:
从前往后和从后往前各跑一遍单调栈,每次求的是以当前值为次大值,之前一个比它大的值为最大值,然后这两个值异或。因为单调栈求的就是这个数向左或者向右找第一个比它大的值的位置。所以这个数的位置和栈顶元素的位置构成的区间中,这个数和栈顶元素就是该区间里的次大值和最大值。
附代码:
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<stack> using namespace std; const int maxn=1e5+10; int ans; stack<int>s; stack<int>s2; int a[maxn]; int main() {int n;scanf("%d",&n);for(int i=0;i<n;i++){scanf("%d",&a[i]);}for(int i=0;i<n;i++){while(!s.empty()&&a[s.top()]<a[i]){s.pop();}if(!s.empty())ans=max(ans,a[i]^a[s.top()]);s.push(i);}for(int i=n-1;i>=0;i--){while(!s2.empty()&&a[s2.top()]<a[i]){s2.pop();}if(!s2.empty()){ans=max(ans,a[i]^a[s2.top()]);}s2.push(i);}printf("%d\n",ans);return 0; }
第二种做法:
%大佬,思路差不多,只是只用求一遍单调栈就可以。从后往前求一遍单调栈然后记录一下对于每一个值它右边第一个比它大的值的位置。然后遍历一遍,每次可以同时求出该数作为最大值和次大值的异或值。
#include<iostream> #include<cstdio> #include<cstring> #include<stack> #include<algorithm> using namespace std; const int maxn=1e5+10; stack<int> s; int r[maxn]; int a[maxn]; int main() {int n,ans;scanf("%d",&n);for(int i=0;i<n;i++)scanf("%d",&a[i]);for(int i=n-1;i>=0;i--){while(!s.empty()&&a[s.top()]<a[i])//找每个数右边第一个比它大的数的下标 {s.pop();}if(s.empty()){r[i]=n;}else{r[i]=s.top();}s.push(i);}ans=0;int t;for(int i=0;i<n;i++){int j=i+1;while(j<n){t=a[i]^a[j];if(t>ans){ans=t;}if(a[j]>a[i]){break;}j=r[j];}} printf("%d\n",ans);return 0; }
转载于:https://www.cnblogs.com/1013star/p/9985414.html
Maximum Xor Secondary(单调栈好题)相关推荐
- Maximum Xor Secondary CodeForces - 281D (单调栈)
Bike loves looking for the second maximum element in the sequence. The second maximum element in the ...
- 洛谷P2866 [USACO06NOV]糟糕的一天Bad Hair Day(单调栈)
题目描述 Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self ...
- 【2019牛客暑期多校训练营(第二场) - H】Second Large Rectangle(单调栈,全1子矩阵变形)
题干: 链接:https://ac.nowcoder.com/acm/contest/882/H 来源:牛客网 题目描述 Given a N×MN \times MN×M binary matrix. ...
- *【HDU - 1506】【POJ - 2559】Largest Rectangle in a Histogram(单调栈或动态规划)
题干: Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...
- bzoj 1657: [Usaco2006 Mar]Mooo 奶牛的歌声(单调栈)
1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 848 Solved: 594 [Sub ...
- 给一个长度为n的字符串,找出长度为m的最小字典子序列【单调栈】
一个大大的分割线,如果这个傻逼题没有被作为某某复赛的签到题,可能我一会都一直傻逼下去了. [2019计蒜之道复赛--星云系统] 题目是,给出一个长度为n(1<n<5e6)的只有小写字母的字 ...
- 入门经典_Chap08_题解总结:极角扫描法 滑动窗口 单调队列 单调栈
总结 本章主要关注一个重要的问题 – 单调队列和单调栈的使用 同时还有一些其他的问题,如扫描法,递归的思想, 构造, 分治, 二分等 知识点 单调队列 和 单调栈 题目 UVA - 1606 Am ...
- 【BZOJ4826】【HNOI2017】影魔(扫描线,单调栈)
[BZOJ4826][HNOI2017]影魔(扫描线,单调栈) 题面 BZOJ 洛谷 Description 影魔,奈文摩尔,据说有着一个诗人的灵魂.事实上,他吞噬的诗人灵魂早已成千上万.千百年来,他 ...
- [数据结构]——单调栈
单调栈 笔者在做leetcode的题(下一个出现的最大数字)时,接触到了单调栈这一种数据结构,经过研究之后,发现单调栈在解决某些问题时出奇的好用,下面是对单调栈的性质和一些典型题目. 什么是单调栈? ...
最新文章
- JavaScript初学者应注意的七个细节
- NUC1003 Hangover
- hdu1074 状态压缩dp+记录方案
- 解决问题:Couldn't open file: data/coco.names
- (73)FPGA面试题-Verilog实现5人表决器
- 包括循环和分支的C语言程序,《C语言程序设计》分支和循环的C程序设计.ppt
- eclipse乱码解决
- java default类型_java – 带有限定符@Default的ZZZZ类型的不满意依赖项
- offsetLeft,Left,clientLeft的区别
- libsvm python Linux Ubuntu下编程操作实践
- CF991B Getting an A
- mysql命令语句连接数据库_MySQL_MySQL常用基本SQL语句总结,1. 常见命令连接本地数据库 - phpStudy...
- 表t_od_use_cnt中没有hour字段,所以hour我们直接
- 《HBase权威指南》一3.4 行锁
- 毕业论文中word的使用1-代码域标公式
- C语言求sgn函数,sgnx(sgn函数)
- 怎么给自己的电脑连接打印机
- STM32的脉冲宽度调制(PWM)
- 最菜的我打卡的第二天
- SpringCloud系列之六
热门文章
- Python切片各种情况详解
- 二、计算机视觉与卷积神经网络
- ai人工智能收入_人工智能促进收入增长:使用ML推动更有价值的定价
- 12家股份银行当中,哪个盈利能力和口碑是最好的?
- 比亚迪:2月新能源汽车销量14429辆 同比增长73%
- UART协议驱动设计
- android 开启wifi失败,[求助]获取基站/wifi信息为空或失败
- RequestBody获取前端数据_360视频云Web前端HEVC播放器实践剖析
- html怎样添加css样式,html添加css样式的方法
- oracle备份 ram,Oracle备份时出现AIX系统的3D32B80D错误