日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Maximum Xor Secondary CodeForces - 281D (单调栈)

發布時間:2023/12/18 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Maximum Xor Secondary CodeForces - 281D (单调栈) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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}.

題意:
給你一個含有n個數的數組,讓你找一個連續的區間,這個區間中的最大值異或上次大值得到的數值最大。
思路:

我們可以知道,一個數a[i] ,最多可以當兩個區間的有效次大值

即a[i] 左邊右邊第一個比a[i] 大的數,與a[i] 構成的2個區間。

那么我們可以維護一個單調遞減的單調棧,棧中每一個數 a[i] 左邊的數就是數組中左邊第一個比a[i]大的數,

把a[i] 從棧中彈出的數,就是 在數組中 a[i] 右邊第一個比a[i] 大的數。

這樣我們就可以把所有有效的區間中最大值和次大值都得出,更新答案即可。

細節見代碼:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #include <iomanip> #define ALL(x) (x).begin(), (x).end() #define sz(a) int(a.size()) #define all(a) a.begin(), a.end() #define rep(i,x,n) for(int i=x;i<n;i++) #define repd(i,x,n) for(int i=x;i<=n;i++) #define pii pair<int,int> #define pll pair<long long ,long long> #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define MS0(X) memset((X), 0, sizeof((X))) #define MSC0(X) memset((X), '\0', sizeof((X))) #define pb push_back #define mp make_pair #define fi first #define se second #define eps 1e-6 #define gg(x) getInt(&x) #define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl using namespace std; typedef long long ll; ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;} ll lcm(ll a, ll b) {return a / gcd(a, b) * b;} ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2) { ans = ans * a % MOD; } a = a * a % MOD; b /= 2;} return ans;} inline void getInt(int *p); const int maxn = 1000010; const int inf = 0x3f3f3f3f; /*** TEMPLATE CODE * * STARTS HERE ***/ int n; ll a[maxn]; stack<ll> st; int main() {//freopen("D:\\code\\text\\input.txt","r",stdin);//freopen("D:\\code\\text\\output.txt","w",stdout);gbtb;cin >> n;repd(i, 1, n) {cin >> a[i];}ll ans = 0ll;repd(i, 1, n) {if (st.empty()) {st.push(a[i]);}else{while(st.size()&&st.top()<a[i]){ans=max(ans,(st.top()^a[i]));st.pop();}if(st.size())ans=max(ans,(st.top()^a[i]));st.push(a[i]);}}ll x;if(st.size()){x=st.top();st.pop();}while(st.size()){ans=max(ans,(x^st.top()));x=st.top();st.pop();}cout<<ans<<endl;return 0; }inline void getInt(int *p) {char ch;do {ch = getchar();} while (ch == ' ' || ch == '\n');if (ch == '-') {*p = -(getchar() - '0');while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 - ch + '0';}} else {*p = ch - '0';while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 + ch - '0';}} }

轉載于:https://www.cnblogs.com/qieqiemin/p/11434777.html

總結

以上是生活随笔為你收集整理的Maximum Xor Secondary CodeForces - 281D (单调栈)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。