求数组中最大的异或值 20171013

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 = -999
for 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);
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1~n数组的最大异或和也可以使用类似于一段区间最大异或和的方法。具体步骤如下: 1. 将1~n数组中的所有数以二进制形式插入到字典树中。 2. 对于每个数,从高位到低位依次匹配字典树上的节点,如果当前位为1,就往字典树的右子树走,否则就往左子树走。匹配完整个二进制数后,我们可以得到一个最大异或。 3. 对于1~n数组,我们可以将其中的相邻两个数看作一段区间,然后使用类似于一段区间最大异或和的方法最大异或和。 时间复杂度为O(nlogC),其中n为数组长度,C为数的范围。以下是1~n数组的最大异或和的C++代码: ```c++ #include <iostream> using namespace std; const int MAXN = 100010; const int MAXBITS = 30; struct TrieNode { int cnt; int children[2]; } trie[MAXN * MAXBITS]; int root, node_cnt; void insert(int x) { int p = root; for (int i = MAXBITS - 1; i >= 0; i--) { int idx = (x >> i) & 1; if (!trie[p].children[idx]) { trie[p].children[idx] = ++node_cnt; } p = trie[p].children[idx]; trie[p].cnt++; } } int query(int x) { int p = root, res = 0; for (int i = MAXBITS - 1; i >= 0; i--) { int idx = (x >> i) & 1; if (trie[trie[p].children[idx ^ 1]].cnt > 0) { res += (1 << i); p = trie[p].children[idx ^ 1]; } else { p = trie[p].children[idx]; } } return res; } int main() { int n; cin >> n; root = 1; node_cnt = 1; int pre = 0, ans = 0; for (int i = 1; i <= n; i++) { int x; cin >> x; insert(pre); pre ^= x; ans = max(ans, query(pre)); } cout << ans << endl; return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值