Codeforces Round #173 (Div. 2) E. Sausage Maximization —— Trie树 + 前缀和

来源:互联网 发布:淘宝0秒付款 编辑:程序博客网 时间:2024/06/05 00:51

题目链接:http://codeforces.com/problemset/problem/282/E


E. Sausage Maximization
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The Bitlandians are quite weird people. They have their own problems and their own solutions. They have their own thoughts and their own beliefs, they have their own values and their own merits. They have their own dishes and their own sausages!

In Bitland a sausage is an array of integers! A sausage's deliciousness is equal to the bitwise excluding OR (the xor operation) of all integers in that sausage.

One day, when Mr. Bitkoch (the local cook) was going to close his BitRestaurant, BitHaval and BitAryo, the most famous citizens of Bitland, entered the restaurant and each ordered a sausage.

But Mr. Bitkoch had only one sausage left. So he decided to cut a prefix (several, may be zero, first array elements) of the sausage and give it to BitHaval and a postfix (several, may be zero, last array elements) of the sausage and give it to BitAryo. Note that one or both pieces of the sausage can be empty. Of course, the cut pieces mustn't intersect (no array element can occur in both pieces).

The pleasure of BitHaval and BitAryo is equal to the bitwise XOR of their sausages' deliciousness. An empty sausage's deliciousness equals zero.

Find a way to cut a piece of sausage for BitHaval and BitAryo that maximizes the pleasure of these worthy citizens.

Input

The first line contains an integer n (1 ≤ n ≤ 105).

The next line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 1012) — Mr. Bitkoch's sausage.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cincout streams or the %I64dspecifier.

Output

Print a single integer — the maximum pleasure BitHaval and BitAryo can get from the dinner.

Examples
input
21 2
output
3
input
31 2 3
output
3
input
21000 1000
output
1000


题解:

1.预处理前缀异或、后缀异或。

2.枚举每一个前缀异或(i:0~n):

2.1.将此前缀异或加入到Trie树中,

2.2.根据后一位的后缀异或,在Trie树中查找与之异或后的最大值,并一直更新ans。



学习之处:

当需要在一个序列的中间删除若干个连续元素,使得满足xx条件时:

1.预处理出前缀和、后缀和。

2.枚举每一个前缀和:将此前缀和插入某种数据结构中,再用后一位的后缀和在此数据结构中查找。

类似的题目:http://blog.csdn.net/dolfamingo/article/details/71001021




代码如下:

#include<bits/stdc++.h>using namespace std;typedef long long LL;const double eps = 1e-6;const int INF = 2e9;const LL LNF = 9e18;const int mod = 1e9+7;const int maxn = 1e5+10;typedef struct node{    struct node *next[2];}Node, *Trie;Trie T;LL n, a[maxn], pre[maxn], rev[maxn];void init(){    scanf("%I64d",&n);    for(int i = 1; i<=n; i++)        scanf("%I64d",&a[i]);    for(int i = 1; i<=n; i++)   //前缀        pre[i] = pre[i-1]^a[i];    for(int i = n; i>=1; i--)   //后缀        rev[i] = rev[i+1]^a[i];    T = new Node;   //初始化Trie树    T->next[0] = T->next[1] = NULL;}void add(LL x){    Trie p = T;    for(int i = 50; i>=0; i--)  //从高位到低位    {        int d = (x>>i)&1;        if(p->next[d]==NULL)    //此位的d不存在, 则新建            p->next[d] = new Node, p->next[d]->next[0] = p->next[d]->next[1] = NULL;        p = p->next[d];    }}LL query(LL x){    LL tmp = 0;    Trie p = T;    for(int i = 50; i>=0; i--)    {        //如果!d路存在,则可加上(d ^ !d = 1), 并顺着这条路走下去; 否则走d路(!d路和d路至少一路存在)        int d = (x>>i)&1;        if(p->next[!d]) tmp += 1LL*(1LL<<i), p = p->next[!d];        else p = p->next[d];    }    return tmp;}void solve(){    LL ans = 0;    for(int i = 0; i<=n; i++) //i为0时, 没有前缀;i为n时,没有后缀。    {        add(pre[i]);        ans = max( ans, query(rev[i+1]) );    }    printf("%I64d\n",ans);}int main(){    init();    solve();}

阅读全文
0 0
原创粉丝点击