求数组中最大的异或值 20171013
来源:互联网 发布:济南java招聘微信群 编辑:程序博客网 时间:2024/06/05 16:46
Suppose you have an integer sequence, we define a lucky number as a special integer which is the XOR value of the sequence’s greatest and strict second greatest element. For example, in [1,2,3,3], 3 is the greatest element and 2 is the strict second greatest one, even though we have two 3 at the same time; so the lucky number is 2 xor 3 = 1.
Now given an N-length integer sequence, please find out the maximum lucky number of all continuous subsequences.
输入描述:
The first line is an integer n, representing the length of the value of V[i](1 <= V[i] <= 10^8) in the sequence respectively. We’ll ensure that there are at least two different values from V[1] to V[n. Define the continuous subsequence as [V[1],V[l+1],V[l+2], … ,V[r]] ( 1 <= l <= r <= n)
30% small input: 2 <= n <= 10
40% medium input: 2 <= n <= 5000
30% large input: 2 <= n <= 100000
输出描述:
Output the greatest lucky number
示例1:
输入
5
5 2 1 4 3
输出
7
python代码
array_length = raw_input()array = raw_input()num = [int(n) for n in array.split()]q = []ans = -999for value in num: while q and q[-1] <= value:#最大的情况 ans = max(ans,q[-1]^value) q.pop() if q:#次大的情况 ans = max(ans,q[-1]^value) q.append(value)print ans
c++代码
#include<iostream>#include<stdio.h>#include<deque>using namespace std;deque<int>q;int main(){ int i, x, ans = -999,n; scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d",&x); while(!q.empty()&&q.back()<=x) { ans = max(ans,q.back()^x); q.pop_back(); } if(q.size()>1) ans= max(ans,q.back()^x); q.push_back(x); } printf("%d",ans);}
- 求数组中最大的异或值 20171013
- 字典树的应用:求数组中异或最大的两个数
- 求数组中最大的值
- 求数组中最大K个值的下标
- 求数组中第K个最大的值
- 求数组中最大的k个数
- 求数组中最大的K个数
- 求二维数组最大子数组和(或矩阵的最大子矩阵和)
- 求二维数组中子数组和中最大的值,及子数组
- 字典树(Trie、prefix tree)及其应用(求一个数组中的最大异或值)
- 求数组中连续子数组的最大和
- 求无序数组中(max+min)最大的子数组
- 求二维数组中最大子数组的和
- 求一个数组中连续子数组的最大和
- 求一个数组中连续子数组的最大乘积
- 求数组中最大连续子数组的和
- 数组中求最大数
- 数组中求最大最小值
- 换博客啦!!!
- 求积问题[体会Python至简之道]
- C#委托的异步调用
- 第二次作业答案(官方)
- [BZOJ4785][ZJOI2017]树状数组(树套树)
- 求数组中最大的异或值 20171013
- 下载 sqoop-1.4.4.bin__hadoop-2.0.4-alpha.tar.gz地址
- 笔试_公司(1)
- AndroidStudio导入项目在 Building gradle project info 一直卡住
- 第三次作业答案(官方)
- 关于win和linux双系统,系统时间出错修复
- java基础(九)之深入剖析线程同步Synchronized,Lock
- 关于对getchar一些小小的自学理解。。。
- 160个CrackMe之51