zoukankan      html  css  js  c++  java
  • HackerRank "AND xor OR"

    Actually I think problem statement is somewhat misleading. No need to mention range [L, R] at all.

    The intention is a variation to "Largest Rectangle" which is a classic stack problem on LeetCode.

    But you need to run Largest Rectangle twice: increased and decreased.

    #include <cmath>
    #include <stack>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    
    int main() {
        int n; cin >> n;
        vector<int> in(n);
        for(int i = 0; i < n; i ++)
            cin >> in[i];
        
        int ret = 0;
        {
            in.push_back(0); 
            stack<int> stk;
            for (int i = 0; i < in.size(); i++)
            {
                if (stk.empty() || in[i] > stk.top())
                {
                    stk.push(in[i]);
                }
                else
                {
                    int lastV = stk.top(); stk.pop();
                    if (!stk.empty())
                    {
                        ret = std::max(ret, (lastV ^ stk.top()));            
                        //cout << lastV << "-" << stk.top() << "=" << (lastV ^ stk.top()) << endl;
                    }
                    i--;
                }
            }
            in.pop_back();
        }
        {
            
            stack<int> stk;
            in.insert(in.begin(), 0);
            for (int i = in.size() - 1; i >= 0; i --)
            {
                if (stk.empty() || in[i] > stk.top())
                {
                    stk.push(in[i]);
                }
                else
                {
                    int lastV = stk.top(); stk.pop();
                    if (!stk.empty())
                    {
                        ret = std::max(ret, (lastV ^ stk.top()));            
                        //cout << lastV << "-" << stk.top() << "=" << (lastV ^ stk.top()) << endl;
                    }
                    i++;
                }
            }
        }
        
        cout << ret << endl;
        return 0;
    }
    View Code
  • 相关阅读:
    过滤非GBK字符
    打印整数数字
    std::string 方法列表
    设计模式——单例模式(Singleton )
    编程之美二进制一的个数
    Jsoncpp试用指南
    GCC下宏扩展后的++i
    关于字节对齐的sizeof的讨论
    Notepad++ 更改和定制主题
    求子数组的最大和
  • 原文地址:https://www.cnblogs.com/tonix/p/4988677.html
Copyright © 2011-2022 走看看