Codeforces Round #172 (Div. 1) BMaximum Xor Secondary 单调栈

//给一个长度为N的个不相同的序列,找出所有区间中最大值和第二大数的异或值最大的值

//对于所有区间只需要找其最大值和第二大数,所以对于很多区间的结果是重复的

//对于每一个数,它起作用的区间只有在其前面最多只有一个数是大于它的

//可以用一个单调递减栈来做,对于每一个新的数a[i],在它前面第一个大于它的数a[j]

//和第二个大于它的数之间的数到a[i]的区间的数的最大值和第二大数为a[j] , a[i]

//只需要找a[i],a[j]的所有区间所有情况

#include<cstdio>

#include<cstring>

#include<iostream>

using namespace std;

const int maxn = 100010 ;

int s[maxn] ;

int main()

{

int n ;

while(~scanf("%d" , &n))

{

int top =  -1 ;

int ans = 0 ;

for(int i = 1;i <= n;i++)

{

int t ;

scanf("%d"  , &t) ;

while(top!=-1 && s[top] < t){ans = max(ans , t^s[top--]) ;}

if(top!=-1)ans = max(ans , t^s[top]) ;

s[++top] = t ;

}

printf("%d\n" , ans) ;

}

return 0 ;

}

时间: 2024-10-08 10:04:06

Codeforces Round #172 (Div. 1) BMaximum Xor Secondary 单调栈的相关文章

Codeforces Round #305 (Div. 2)D---Mike and Feet(单调栈)

Mike is the president of country What-The-Fatherland. There are n bears living in this country besides Mike. All of them are standing in a line and they are numbered from 1 to n from left to right. i-th bear is exactly ai feet high. A group of bears

Codeforces Round #333 (Div. 1)--B. Lipshitz Sequence 单调栈

题意:n个点, 坐标已知,其中横坐标为为1~n. 求区间[l, r] 的所有子区间内斜率最大值的和. 首先要知道,[l, r]区间内最大的斜率必然是相邻的两个点构成的. 然后问题就变成了求区间[l, r]内所有子区间最大值的和. 这个问题可以利用单调栈来做. 每次找到当前点左面第一个大于当前值的点, 然后更新答案. 姿势很多. 1 import java.io.BufferedInputStream; 2 import java.io.BufferedOutputStream; 3 import

Maximum Xor Secondary(单调栈好题)

Maximum Xor Secondary CodeForces - 280B Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1,?x2,?...,?xk (k?>?1) is such maximum element xj, that the following inequalit

Codeforces Round #172 (Div. 2)---D. Maximum Xor Secondary(RMQ + 二分)

Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1,?x2,?-,?xk (k?>?1) is such maximum element xj, that the following inequality holds: . The lucky number of the sequenc

Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)

题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题是线段树成段更新,但是不能直接更新,不然只能一个数一个数更新.这样只能把每个数存到一个数组中,长度大概是20吧,然后模拟二进制的位操作.仔细一点就行了. 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath>

Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 【莫队算法 + 异或和前缀和的巧妙】

任意门:http://codeforces.com/problemset/problem/617/E E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Bob has a favorite number k and ai of length n. Now he asks yo

Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 莫队算法

E. XOR and Favorite Number Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is given by a pair li and ri and asks you to count the number of pairs of integers i and j, such that l ≤ i ≤ j ≤ r and the xor

Codeforces Round #315 (Div. 1)

A. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and un

Codeforces Round #354 (Div. 2) ABCD

Codeforces Round #354 (Div. 2) Problems # Name     A Nicholas and Permutation standard input/output 1 s, 256 MB    x3384 B Pyramid of Glasses standard input/output 1 s, 256 MB    x1462 C Vasya and String standard input/output 1 s, 256 MB    x1393 D T