281D - Maximum Xor Secondary - CodeForces Solution


two pointers *1800

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <bits/stdc++.h>
#define IOS                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
#define fast_io std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define MOD 1000000007
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<vector<long long>> vvll;
typedef vector<long long> vll;
typedef vector<string> vs;

int main()
{
    // IOS;
    // #ifndef ONLINE_JUDGE
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    // #endif
    ll n;
    cin>>n;
    vll a(n);

    for(int i=0;i<n;i++)
    cin>>a[i];

    vll ngr(n), ngl(n);

    stack<ll> st;

    for(int i=0;i<n;i++){
        while(!st.empty() && a[st.top()]<=a[i]){
            st.pop();
        }

        if(st.empty()){
            ngl[i] = i;
        }
        else{
            ngl[i] = st.top();
        }

        st.push(i);
    }

    st = stack<ll>();

    for(int i=n-1;i>=0;i--){
        while(!st.empty() && a[st.top()]<=a[i]){
            st.pop();
        }

        if(st.empty()){
            ngr[i] = i;
        }
        else{
            ngr[i] = st.top();
        }

        st.push(i);
    }

    ll res = 0;

    for(int i=0;i<n;i++){
        ll temp = max(a[i]^a[ngl[i]], a[i]^a[ngr[i]]);
        res = max(res, temp);
    }

    cout<<res;
}


Comments

Submit
0 Comments
More Questions

1191A - Tokitsukaze and Enhancement
903A - Hungry Student Problem
52B - Right Triangles
1712A - Wonderful Permutation
1712D - Empty Graph
1712B - Woeful Permutation
1712C - Sort Zero
1028B - Unnatural Conditions
735B - Urbanization
746C - Tram
1278B - A and B
1353D - Constructing the Array
1269C - Long Beautiful Integer
1076A - Minimizing the String
913C - Party Lemonade
1313A - Fast Food Restaurant
681A - A Good Contest
1585F - Non-equal Neighbours
747A - Display Size
285A - Slightly Decreasing Permutations
515C - Drazil and Factorial
1151E - Number of Components
1151F - Sonya and Informatics
556A - Case of the Zeros and Ones
867A - Between the Offices
1569A - Balanced Substring
260A - Adding Digits
1698C - 3SUM Closure
1029B - Creating the Contest
1421A - XORwice