B. Maximum Xor Secondary(单调栈)

 

有 n 个数,求在任意长度区间内的最大值 a[i] 与次大值 a[j] 的异或值最大是多少 

利用栈顶最大的单调栈维护次大值找到区间内的最大值即可 

const int N=2e5+5;
 
    int n,m,t;
    int i,j,k;
    ll a[N];
    stack<int> s;

int main()
{
    //IOS;
    while(sd(n)==1){
        for(i=1;i<=n;i++) sd(a[i]);
        ll ans=0;
        for(i=1;i<=n;i++){
            while(s.size() && a[s.top()]<=a[i]) s.pop();
            if(s.size()) ans=max(ans,a[i]^a[s.top()]);
            s.push(i);
        }
        stack<int> hollow;
        swap(hollow,s);
        for(i=n;i;i--){
            while(s.size() && a[s.top()]<=a[i]) s.pop();
            if(s.size()) ans=max(ans,a[i]^a[s.top()]);
            s.push(i);
        }
        pll(ans);
    }
    //PAUSE;
}

 

 

猜你喜欢

转载自blog.csdn.net/C_Dreamy/article/details/107730909