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. Tomcat集群---Cluster节点配置(转)

    <!-- Cluster(集群,族) 节点,如果你要配置tomcat集群,则需要使用此节点. className 表示tomcat集群时,之间相互传递信息使用那个类来实现信息之间的传递. cha ...

  2. 一个for列出横纵坐标

    h = i % * hCount; v = Math.floor(i / hCount);

  3. Spark延长SparkContext初始化时间

    有些应用中可能希望先在driver上运行一段java单机程序,然后再初始化SparkContext用集群模式操作java程序返回值.从而避免过早建立SparkContext对象分配集群资源,使资源长时 ...

  4. 蓝牙SIG

    蓝牙SIG 蓝牙SIG是一个国际性的非营利组织,它的目的是制定蓝牙的技术规范和推广蓝牙技术的应用.该组织由发起会员(Promoter).合作会员(Associate Member)和接受会员(Adop ...

  5. python 的一些小技巧

    赋值: a, b, c = 'xixi', 'haha', 'hehe' 连接字典: >>> s = {1:'a', 2:'b', 3:'c'} >>> s.key ...

  6. 1password密码文件重装后恢复

    因为重装系统,加上Time machine硬盘损坏. 只能从之前零散Time machine的恢复数据中找到最近的一个备份. 1password是会自己执行备份的,起备份文件在 ~/Library/C ...

  7. POJ 2387 Til the Cows Come Home

    题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K ...

  8. ZOJ 1240 IBM Minus One

    /* You may have heard of the book '2001 - A Space Odyssey' by Arthur C. Clarke, or the film of the s ...

  9. 基于时间延迟的Python验证脚本

    自己写的一段Python脚本,经常拿来验证一些sqlmap等工具跑不出数据的网站. GET类型: import urllib import urllib2 import time payloads = ...

  10. centos6.3 + db2v9.7的数据库移行

    工作内容如题,我要做的事情大体如下: 1,正确备份可用数据库: 2,安装64位的cent os 6.3: 3,将1备份的数据恢复到新的cent os 6.3系统上. 第一件事情,就是备份一个可用的数据 ...