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

Input
5
5 2 1 4 3
Output
7
Input
5
9 8 3 5 7
Output
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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/250224.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/250224.shtml
英文地址,請注明出處:http://en.pswp.cn/news/250224.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python --- udp的使用

1. python的模塊導入規則 參考 1.1 系統自帶模塊 系統自帶的模塊直接import導入 import time import unittest1.2 第三方下載模塊 第三方下載模塊也可以直接導入 import HTMLTestRunner import requests1.3 導入模塊的部分函數或類 from time import sleep,strftime fro…

雜項-公司:唯品會

ylbtech-雜項-公司&#xff1a;唯品會唯品會公司成立于2008年08月&#xff0c;2012年3月23日登陸美國紐約證券交易所上市&#xff08;股票代碼&#xff1a;VIPS&#xff09;。成為華南第一家在美國紐交所上市的電子商務企業。主營B2C商城唯品會名牌折扣網站是一家致力于打造中高…

python --- 使用socket創建tcp服務

1. 網絡-tcp 參考 1.1 tcp簡介 介紹 TCP協議,傳輸控制協議(英語: Transmission Control Protocol, 縮寫為TCP)是一種面向連接的、可靠的、基于字節流的傳輸層通信協議,由IETF的RFC 793定義. TCP通信需要經過創建連接、數據傳送、終止連接三個步驟. TCP通信模型中,在通信開…

Linux基本的操作

一、為什么我們要學習Linux 相信大部分人的PC端都是用Windows系統的&#xff0c;那我們為什么要學習Linux這個操作系統呢&#xff1f;&#xff1f;&#xff1f;Windows圖形化界面做得這么好&#xff0c;日常基本使用的話&#xff0c;學習成本幾乎為零。 而Linux不一樣&#xff…

匯編語言 實驗4

實驗4 實驗內容1&#xff1a;綜合使用 loop,[bx]&#xff0c;編寫完整匯編程序&#xff0c;實現向內存 b800:07b8 開始的連續 16 個 字單元重復填充字數據 0403H&#xff1b;修改0403H為0441H&#xff0c;再次運行 步驟1&#xff1a;在記事本中編寫好temp.asm文件 步驟2&#x…

python --- 線程

1. 多任務 - 線程 參考 首先考慮一個沒有多任務的程序: import timedef sing():# 唱歌 5 秒鐘for i in range(5):print("-----菊花臺ing....-----")time.sleep(1)def dance():# 跳舞 5秒鐘for i in range(5):print("-----跳舞.....-----")time.sleep(5)d…

Python 鏈接匯總

MNIST手寫識別 轉載于:https://www.cnblogs.com/bycnboy/p/9095199.html

17種常用的JS正則表達式 非負浮點數 非負正數

<input typetext idSYS_PAGE_JumpPage nameSYS_PAGE_JumpPage size3 maxlength5 οnkeyupthis.valuethis.value.replace(/[^1-9]\D*$/,"") οndragenter"return false" οnpaste"return !clipboardData.getData(text).match(/\D/)"" sty…

python --- 使用conda配置pytorch

使用Conda配置PyTorch 1. 添加channels 下載地址 $ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ $ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ $ conda config --add channels htt…

LDAP第三天 MySQL+LDAP 安裝

https://www.easysoft.com/applications/openldap/back-sql-odbc.html OpenLDAP 使用 SQLServer 和 Oracle 數據庫。 https://www.cnblogs.com/bigbrotherer/p/7251372.html          CentOS7安裝OpenLDAPMySQLPHPLDAPadmin 1.安裝和設置數據庫 在CentOS7下&…

Myeclipse連接Mysql數據庫時報錯:Error while performing database login with the pro driver:unable...

driver template: Mysql connector/j&#xff08;下拉框進行選擇&#xff09; driver name: 任意填&#xff0c;最好是數據庫名稱&#xff0c;方便查找 connection URL: jdbc:mysql://localhost:3306/programmableweb User name: 用戶名 password: 密碼 Driver jars: 添加jar包…

Centos6.5靜態IP設置

1.創建新的虛擬機 2.打開終端&#xff0c;打開/etc/sysconfig/network-scripts/ifcfg-eth0文件 3.將BOOTPROTOstatic&#xff0c;原值為dhcp 4.添加 IPADDR192.168.43.125  #靜態IP GATEWAY192.168.43.1  #網關 NETMASK255.255.255.0  #子網掩碼 NETWORK192.168.43.0  …

matlab --- 圖像處理基礎

MATLAB圖像處理 1. 數字圖像處理 參考 數字圖像處理(Digital Image Processing)又稱為計算機圖像處理,是一種將圖像信號數字化利用計算進行處理的過程。隨著計算機科學、電子學和光學的發展,數字圖像處理已經廣泛的應用到諸多領域之中。本小節主要介紹圖像的概念、分類和數字…

java 注解默認值

package com.zejian.annotationdemo;import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;/** * Created by wuzejian on 2017/5/19. * 數據類型使用Demo */T…

[python、flask] - POST請求

1. 微信小程序POST傳遞數據給flask服務器 小程序端 // 提交POST數據 import { request } from "../../request/index.js"async handleDetectionPoints() {let params {url: "/detect_points",data: {"points": arr,"img_name": thi…

[vue]data數據屬性及ref獲取dom

data項的定義 this.$refs獲取dom 獲取不到數據 這樣中轉下才ok 小結: data里不能用this.$ref. 另外使用visjs時候 view-source:http://visjs.org/examples/network/basicUsage.html 加載不出東西,點了按鈕觸發才ok 小結: create里應該是從上到下執行的. 轉載于:https://www.cnb…

Linux命令基礎3

1. 計劃任務&#xff1a;分為”一次性“ 和”長期性“ 一次性任務是由atq服務/進程來實現的&#xff0c;計劃的管理操作是at命令&#xff1a; at <時間> : 安排一次性任務 atq 或at -l &#xff1a; 查看任務列表 at -c 序號&#xff1a; 預覽任務與設置環境 atrm 序號…

[異步、tensorflow] - 子線程操作tensor,主線程處理tensor

參考整體流程如下圖 代碼 import tensorflow as tf"""模擬: 子線程不停的取數據放入隊列中, 主線程從隊列中取數據執行包含: 作用域的命名、把程序的圖結構寫入事件、多線程 """# 模擬異步存入樣本. # 1、 定義一個隊列,長度為1000 with tf.va…

Element

官網&#xff1a;http://element-cn.eleme.io/#/zh-CN 轉載于:https://www.cnblogs.com/weibanggang/p/9995433.html

ubuntu18.04下安裝Anaconda及numpy、matplotlib

為了學習深度學習&#xff0c;我需要首先掌握利用python進行科學計算的知識&#xff0c;順便復習一下線性代數、微積分、概率論。當然&#xff0c;現在我要做的是安裝Anaconda。 1、官網下載&#xff0c;linux版本&#xff1a;https://www.anaconda.com/download 2、如果太慢&a…