51nod1423 最大二“货”

[传送门]

单调栈其实就是个后缀$max/min$,这道题可以维护一个单调递减的单调栈,pop元素的时候,能pop掉的元素就是第二大,当前元素为第一大。遇到第一个不能pop掉的时候当前元素就是第二大,不能pop掉的元素就是第一大。

#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 7;
int st[N], top;

int main() {
    int n;
    while (~scanf("%d", &n)) {
        top = 0;
        int ans = 0;
        for (int i = 1, x; i <= n; i++) {
            scanf("%d", &x);
            while (top && st[top] < x) 
                ans = max(ans, x ^ st[top--]);
            if (top)
                ans = max(ans, x ^ st[top]);
            st[++top] = x;
        }
        printf("%d\n", ans);
    }
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/Mrzdtz220/p/11664622.html