CF280B

题目链接

首先如果我们假设次大值已经知道了,那么最大值就可以假设在次大值前面,或者后面,而且最大值只有一个(如果多个就不是这个次大值了),那么这样就可以用单调栈来求解了可以转换成这样

\(\mathscr{Code:}\)

#include<bits/stdc++.h>
#define LL long long
//#define int LL
#define per(i, a, b) for (int i = a, END##i = b; i >= END##i; i--)
#define rep(i, a, b) for (int i = a, END##i = b; i <= END##i; i++)
#define repn(x) rep(x, 1, n)
#define repm(x) rep(x, 1, m)
#define pb push_back
#define e(x) for(int i = h[x], v = to[i]; i; i = nxt[i], v = to[i])
#define E(x) for(auto y : p[x])
#define PII pair<int, int>
#define i64 unsigned long long
#define YY puts("Yes"), exit(0)
#define NN puts("No"), exit(0)
using namespace std;
const int Mod = 1e9 + 7;
const int Inf = 0x3f3f3f3f;
const LL InfLL = 0x3f3f3f3f3f3f3f3f;
inline LL read() {LL s = 0, fu = 1; char ch = getchar(); while (ch < '0' || ch > '9') ch == '-' ? fu = -1 : 0, ch = getchar(); while (ch >= '0' && ch <= '9') s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar(); return s * fu;}

const int N = 1e5 + 10;
int n, s[N], stk[N], top, ans;

inline void Main() {
    n = read();
    repn(i) s[i] = read();
    repn(i) {
        while (top && stk[top] <= s[i]) {
            ans = max(ans, stk[top--] ^ s[i]);
        }
        stk[++top] = s[i];
    }
    top = 0;
    per(i, n, 1) {
        while (top && stk[top] <= s[i]) {
            ans = max(ans, stk[top--] ^ s[i]);
        }
        stk[++top] = s[i];
    }
    printf("%d\n", ans);
}

signed main() {
    // freopen("input.in", "r", stdin);
    int T = 1;
    while (T--)
        Main();
    return 0;
}
posted @ 2025-08-22 18:19  wh2011  阅读(11)  评论(0)    收藏  举报