Exam 2 Maximum Xor with Prefix and Suffix - Works Application 16

15 篇文章 0 订阅
12 篇文章 0 订阅

Maximum Xor with Prefix and Suffix

Description

We have N numbers as an array, you need to find a prefix array and a suffix arrar,which we can get the maximum xor value with all elements in them. Notice that for prefix[0,1] and suffix [r,n-1],do not intersect (l 小于 r), and they can be empty.

Limits

-Memory limit per test : 256 megabytes
-Time limit per test : The faster the better

Compile & Environment

C++

g++ Main.cc –o Main –fno-asm –Wall –lm –static –std=c++0x –DONLINE_JUDGE

Java

J2SE8
Maximum stack size is 50m

Input

The first line is one Number N( 1<=N<=100000)
The second line contains N numbers ai (0 <= ai <=1e12) separated by space,
Which represents the array.

Output

Just output the maximum xor result.

Sample Test

Input

3
1 2 3

Out

3

Code

#include<iostream>
#include <algorithm>

long long pre[100005];
long long suf[100005];
long long arr[100005];
long long res[100005];
using namespace std;

long long cal(long long zu[100005], int m, int n){
    long long res = 0;
    for (int i = m; i <= n; i++){
        res = max(res, zu[i]);
    }
    return res;
}

int main(){
    int N;
    cin >> N;
    int i = 1;
    for (int i = 1; i <= N; i++){
        cin >> arr[i];
    }
    pre[0] = pre[N + 1] = suf[0] = suf[N + 1] = 0;
    for (int i = 1; i<N; i++){
        pre[i] = pre[i - 1] ^ arr[i];
    }
    for (int i = N; i >= 1; i--){
        suf[i] = suf[i + 1] ^ arr[i];
    }
    long long ans = 0;
    for (int i = 1; i <= N; i++){
        ans = max(ans, max(cal(pre, 0, i), cal(suf, i + 1, N)));

    }
    cout << ans << endl;
    return 0;
}

ca3d251f-0e7b-4258-8e68-8f7c9caa676+7

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值