1 条题解

  • 1
    @ 2024-3-13 11:25:11

    根据单调栈的性质可知,单调栈中的元素即为所有后缀最大值。入栈时异或进答案,出栈时再异或一次抵消即可。(本题输入量较大,需要关闭缓存并开O2优化)

    参考代码
    
    #include <iostream>
    using namespace std;
    unsigned long long n, x[1000005], stk[1000005], top, ans;
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        cin >> n;
        for (int i = 1; i <= n; i++) {
            cin >> x[i];
            while (top > 0 && x[i] > x[stk[top]]) {
                ans ^= stk[top];
                top--;
            }
            ans ^= i;
            stk[++top] = i;
            cout << ans << endl;
        }
        return 0;
    }
    
    • 1

    求数列所有后缀最大值的位置

    信息

    ID
    694
    时间
    1000ms
    内存
    256MiB
    难度
    3
    标签
    (无)
    递交数
    71
    已通过
    38
    上传者