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. Bean实例化(Spring源码阅读)-我们到底能走多远系列(33)

    我们到底能走多远系列(33) 扯淡: 各位:    命运就算颠沛流离   命运就算曲折离奇   命运就算恐吓着你做人没趣味   别流泪 心酸 更不应舍弃   ... 主题: Spring源码阅读还在继 ...

  2. 总结 output 用法

    第一种用法 返回受 INSERT.UPDATE 或 DELETE 语句影响的每行的信息,或者返回基于上述每行的表达式.这些结果可以返回到处理应用程序, 以供在确认消息.存档以及其他类似的应用程序要求中 ...

  3. Page_Init 的执行过程

    网上有的资料在说Page_Init这个事件只会在第一次加载页面时触发,实际上并不是这样,我们会发现每次回发页面,Page_Init事件都会被执行, 举一个例子 private static int t ...

  4. autoproxy 规则

    目前在 gfwList 中,有如下的规则格式: example.com 匹配:http://www.example.com/foo匹配:http://www.google.com/search?q=w ...

  5. MySQL数据库恢复的经历。

    蛋疼,定时任务设置错误.把数据给删除了.还有一次是服务器时间不对,也把数据给删除了. 还好,开启了二进制日志,才算把数据找回,但是速度效率也太低. 痛定思变.在把一切交由电脑工作的时候,也要做好一定的 ...

  6. 解决MySQL中【Cannot load from mysql.proc. The table is probably corrupted

    [错误过程]:MySQL从5.1升级至5.5后在调用存储过程时报出“Cannot load from mysql.proc. The table is probably corrupted.” [造成 ...

  7. Mybatis学习 —— 包括所有 mybatis官网

    http://www.mybatis.org/mybatis-3/zh/configuration.html#typeAliases

  8. leetcode 112 Path Sum ----- java

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  9. Codeforces Round #114 (Div. 2)

    Codeforces Round #114 (Div. 2) 代码 Codeforces Round #114 (Div. 2) C. Wizards and Trolleybuses 思路 每条车的 ...

  10. LeetCode(169)Majority Element and Majority Element II

    一个数组里有一个数重复了n/2多次,找到 思路:既然这个数重复了一半以上的长度,那么排序后,必然占据了 a[n/2]这个位置. class Solution { public: int majorit ...