zoukankan      html  css  js  c++  java
  • Maximum Xor Secondary CodeForces

    Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1, x2, ..., xk (k > 1) is such maximum element xj, that the following inequality holds: .

    The lucky number of the sequence of distinct positive integers x1, x2, ..., xk (k > 1) is the number that is equal to the bitwise excluding OR of the maximum element of the sequence and the second maximum element of the sequence.

    You've got a sequence of distinct positive integers s1, s2, ..., sn (n > 1). Let's denote sequence sl, sl + 1, ..., sr as s[l..r] (1 ≤ l < r ≤ n). Your task is to find the maximum number among all lucky numbers of sequences s[l..r].

    Note that as all numbers in sequence s are distinct, all the given definitions make sence.

    Input

    The first line contains integer n (1 < n ≤ 105). The second line contains n distinct integers s1, s2, ..., sn (1 ≤ si ≤ 109).

    Output

    Print a single integer — the maximum lucky number among all lucky numbers of sequences s[l..r].

    Examples

    Input
    5
    5 2 1 4 3
    Output
    7
    Input
    5
    9 8 3 5 7
    Output
    15

    Note

    For the first sample you can choose s[4..5] = {4, 3} and its lucky number is (4 xor 3) = 7. You can also choose s[1..2].

    For the second sample you must choose s[2..5] = {8, 3, 5, 7}.

    求任意区间最大值,用单调递减栈 

    要明确 一个元素最多只有两次当次大值的机会(忽略同一个最大值的不同区间  (左边一个最大值  右边一个最大值))

    可以很容易的想到当前元素在哪个区间是最大值

    设删除元素的下标为id  当前元素为i

    那么a[id]即为a[id, i]这个区间的次大值

    如果向左拓展时删除的最后一个元素的下标为id

    那么a[id - 1]即为 a[i]左边的最大值

    也就是a[i]为a[id - 1]右边的次大值

    对于a[i]它左边的次大值我们都取了一遍

    右边的次大值会乖乖的找到它

    就像a[i]找到a[id - 1]一样

    想一想 是不是这样

    扩展:

      那么我们通过这个思路就可以求出每个元素为次大值的区间

    那么第三大 第四大·····

    #include <iostream>
    #include <cstdio>
    #include <sstream>
    #include <cstring>
    #include <map>
    #include <cctype>
    #include <set>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <algorithm>
    #include <list>
    #include <cmath>
    #include <bitset>
    #define rap(i, a, n) for(int i=a; i<=n; i++)
    #define rep(i, a, n) for(int i=a; i<n; i++)
    #define lap(i, a, n) for(int i=n; i>=a; i--)
    #define lep(i, a, n) for(int i=n; i>a; i--)
    #define rd(a) scanf("%d", &a)
    #define rlld(a) scanf("%lld", &a)
    #define rc(a) scanf("%c", &a)
    #define rs(a) scanf("%s", a)
    #define rb(a) scanf("%lf", &a)
    #define rf(a) scanf("%f", &a)
    #define pd(a) printf("%d
    ", a)
    #define plld(a) printf("%lld
    ", a)
    #define pc(a) printf("%c
    ", a)
    #define ps(a) printf("%s
    ", a)
    #define MOD 2018
    #define LL long long
    #define ULL unsigned long long
    #define Pair pair<int, int>
    #define mem(a, b) memset(a, b, sizeof(a))
    #define _  ios_base::sync_with_stdio(0),cin.tie(0)
    //freopen("1.txt", "r", stdin);
    using namespace std;
    const int maxn = 110000, INF = 0x7fffffff;
    
    stack<LL> S;
    
    int main()
    {
        int n;
        LL tmp;
        LL mx = -INF;
        rd(n);
        for(int i = 1; i <= n; i++)
        {
            rlld(tmp);
            while(!S.empty() && S.top() < tmp)
            {
                mx = max(mx, S.top() ^ tmp);
                S.pop();
            }
            if(!S.empty()) mx = max(mx, tmp ^ S.top());
            S.push(tmp);
        }
        cout << mx << endl;
    
    
        return 0;
    }

    Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1, x2, ..., xk (k > 1) is such maximum element xj, that the following inequality holds: .

    The lucky number of the sequence of distinct positive integers x1, x2, ..., xk (k > 1) is the number that is equal to the bitwise excluding OR of the maximum element of the sequence and the second maximum element of the sequence.

    You've got a sequence of distinct positive integers s1, s2, ..., sn (n > 1). Let's denote sequence sl, sl + 1, ..., sr as s[l..r] (1 ≤ l < r ≤ n). Your task is to find the maximum number among all lucky numbers of sequences s[l..r].

    Note that as all numbers in sequence s are distinct, all the given definitions make sence.

    Input

    The first line contains integer n (1 < n ≤ 105). The second line contains n distinct integers s1, s2, ..., sn (1 ≤ si ≤ 109).

    Output

    Print a single integer — the maximum lucky number among all lucky numbers of sequences s[l..r].

    Examples

    Input
    5
    5 2 1 4 3
    Output
    7
    Input
    5
    9 8 3 5 7
    Output
    15

    Note

    For the first sample you can choose s[4..5] = {4, 3} and its lucky number is (4 xor 3) = 7. You can also choose s[1..2].

    For the second sample you must choose s[2..5] = {8, 3, 5, 7}.

  • 相关阅读:
    317 随笔
    316 随笔
    315 随笔
    python 第一章
    matlab 第四章 第一节 字符串 元胞
    matlab 第三章 第二节 多维数组
    matlab 第三章
    python 循环+break continue
    Springboot 教程 导入
    matlab 第二章 第三节 数值表示、变量及表达式
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/10818037.html
Copyright © 2011-2022 走看看