题意:
给你 n 个不同的数,求任意区间长度内最大的两个数相异或的最大值。
思路:
维护一个单调递减序列,每次pop出的栈顶元素正是以 a[i] 为最大值时(往左匹配到)的次大值,每次push时,栈顶元素正是往左匹配 以这个数为次大值时只能匹配到的最大值,扫一遍就ok了
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
int a[maxn];
int main()
{
int n;
scanf("%d",&n);
stack<int> s;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
int ans = 0;
s.push(a[1]);
for(int i=2; i<=n ;i++)
{
while(!s.empty() && a[i]>s.top())
{
ans = max(ans,a[i]^s.top());
s.pop();
}
if(!s.empty())
ans = max(ans,a[i]^s.top());
s.push(a[i]);
}
printf("%d\n",ans);
return 0;
}