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. python解析smart结构数据

    python编程解析如下smart结构数据,得到一行smart信息 run: smartctl -a /dev/sda out: smartctl 6.3 2014-07-26 r3976 [x86_ ...

  2. spark读hdfs文件实现wordcount并将结果存回hdfs

    package iie.udps.example.operator.spark; import scala.Tuple2; import org.apache.spark.SparkConf; imp ...

  3. ZPPR032-批量展BOM

    *-----------------------------------------------------------------------REPORT zppr032 NO STANDARD P ...

  4. 使用npoi.dll导出数据到excel

    .net数据导出excel数据有多种方法,最常用的就是使用office组件,但随之而来的问题也很棘手,又要调权限又要确定是否安装office很是麻烦,最近一个项目中也有数据导出功能,随使用excel模 ...

  5. php部分---面向对象静态、抽象类、oop接口、加载类、魔术方法、关键字。

    静态  static关键字 普通成员普通成员是属于对象的 静态成员静态成员是属于类的 普通方法里面可以调用静态成员静态方法里面不能调用普通成员self关键字 在类里面代表该类 普通类class Ren ...

  6. leetcode 109 Convert Sorted List to Binary Search Tree ----- java

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  7. timus 1136 Parliament(e)

    Parliament Time limit: 1.0 secondMemory limit: 64 MB A new parliament is elected in the state of MMM ...

  8. iOS应用日志:开始编写日志组件与异常日志

    应用日志(一):开始编写日志组件 对于那些做后端开发的工程师来说,看 LOG解Bug应该是理所当然的事,但我接触到的移动应用开发的工程师里面,很多人并没有这个意识,查Bug时总是一遍一遍的试图重现,试 ...

  9. 使用支持向量机训练mnist数据

    # encoding: utf-8 import numpy as np import matplotlib.pyplot as plt import cPickle import gzip clas ...

  10. python命令行添加Tab键自动补全

    1.编写一个tab的自动补全脚本,名为tab.py #!/usr/bin/python # python tab complete import sys import readline import ...