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 = ; i < n; i ++)
cin >> in[i]; int ret = ;
{
in.push_back();
stack<int> stk;
for (int i = ; 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(), );
for (int i = in.size() - ; i >= ; 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 ;
}

HackerRank "AND xor OR"的更多相关文章

  1. *[hackerrank]Maximizing XOR

    https://www.hackerrank.com/contests/w1/challenges/maximizing-xor/ 找了半天规律,答案竟然是暴力,伤感.我找到的方法是利用规律2^x X ...

  2. 【HackerRank】Maximizing XOR

    给定两个整数:L 和 R ∀ L ≤ A ≤ B ≤ R, 找出 A xor B 的最大值. 输入格式 第一行包含 L 第一行包含 R 数据范围 1 ≤ L ≤ R ≤ 103 输出格式 输出最大的异 ...

  3. 51nod1295 XOR key

    第一次写可持久化trie指针版我... //Null 的正确姿势终于学会啦qaq... #include<cstdio> #include<cstring> #include& ...

  4. 51nod1295 XOR key(可持久化trie)

    1295 XOR key题目来源: HackerRank基准时间限制:1.5 秒 空间限制:262144 KB 分值: 160 难度:6级算法题 给出一个长度为N的正整数数组A,再给出Q个查询,每个查 ...

  5. 51nod 1295 XOR key (可持久化Trie树)

    1295 XOR key  题目来源: HackerRank 基准时间限制:1.5 秒 空间限制:262144 KB 分值: 160 难度:6级算法题   给出一个长度为N的正整数数组A,再给出Q个查 ...

  6. 51Nod XOR key —— 区间最大异或值 可持久化字典树

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1295 1295 XOR key  题目来源: HackerRa ...

  7. [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字

    Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...

  8. 二分+DP+Trie HDOJ 5715 XOR 游戏

    题目链接 XOR 游戏 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  9. BZOJ 2115 【Wc2011】 Xor

    Description Input 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 ...

随机推荐

  1. for else

  2. JAVA学习之Ecplise IDE 使用技巧(2)第二章:键盘小快手,代码辅助

    上一篇:JAVA学习之Ecplise IDE 使用技巧(1)第一章:我的地盘我做主,工作空间 第二章:键盘小快手,代码辅助 内容包括: 第一:显示行号 如何设置行号:Ecplice菜单Windows& ...

  3. poj 1990 MooFest

    题目大意: FJ有n头牛,排列成一条直线(不会在同一个点),给出每头牛在直线上的坐标x.另外,每头牛还有一个自己的声调v,如果两头牛(i和j)之间想要沟通的话,它们必须用同个音调max(v[i],v[ ...

  4. Android高效加载大图,多图解决方案,有效避免程序OOM异常

    收藏自:http://blog.csdn.net/guolin_blog/article/details/9316683 谷歌官方文档:http://developer.android.com/tra ...

  5. Java 正则表达式学习总结和一些小例子

    从Java1.4起,Java核心API就引入了java.util.regex程序包,它是一种有价值的基础工具,可以用于很多类型的文本处理, 如匹配,搜索,提取和分析结构化内容. java.util.r ...

  6. WebApp远程调试工具

    针对存在内嵌在客户端的webview项目中,在开发过程中我们希望能像PC上一下直接进行调试,提高我们平时的开发效率,这时候我们可以使用webApp远程调试工具-weinre. 安装: 本工具需要nod ...

  7. URAL-1989 Subpalindromes(单点更新+hash)

    题目大意:给一行字符串,两种操作:change(pos,char),将pos处字符改为char:isPalindrome(i,j),询问[i,j]之间是否为回文字符串. 题目分析:做正反两次字符串哈希 ...

  8. java 读取文件的字节数组

    /*文件64位编码*/ public static void main(String[] args) {    byte[] fileByte = toByteArray(newFile);   St ...

  9. C# Development 13 Things Every C# Developer Should Know

    https://dzone.com/refcardz/csharp C#Development 13 Things Every C# Developer Should Know Written by ...

  10. Json Null 不输出

    [TestMethod]        public void NodeTest()        {            datanode d = new datanode()           ...