Codeforces 282E Sausage Maximization【字典树+贪心】

来源:互联网 发布:linux 设置目录权限 编辑:程序博客网 时间:2024/05/22 00:28

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 (thexor 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 thecin,cout streams or the%I64d specifier.

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

题目大意:


给一个长度为n的整数序列,现在要你截取这个序列的一个前缀和一个后缀(前缀和后缀不能相交),使得前缀和后缀的异或值最大。


思路:


贪心去做,对于每个查询,同样处理成35位二进制01字符串,对应进行查询,如果当前位子是0,那么尽量往1那边走,同理,如果当前位子是1,那么尽量往0那边走即可。


那么O(n)建树+查询即可。


Ac代码:

#include<stdio.h>#include<string.h>#include<stdlib.h>#include<iostream>using namespace std;#define ll __int64ll a[100002];ll pre[100002];ll back[100002];#define maxn 2typedef struct tree{    tree *nex[maxn];    ll v;    ll val;}tree;ll output;tree root;void init(){    for(ll i=0;i<maxn;i++)    {        root.nex[i]=NULL;    }}void creat(char *str,ll va){    ll len=strlen(str);    tree *p=&root,*q;    for(ll i=0;i<len;i++)    {        ll id=str[i]-'0';        if(p->nex[id]==NULL)        {            q=(tree *)malloc(sizeof(root));            q->v=1;            for(ll j=0;j<2;j++)            {                q->nex[j]=NULL;            }            p->nex[id]=q;        }        else        {            p->nex[id]->v++;        }        p=p->nex[id];        if(i==len-1)        {            p->val=va;        }    }}void find(char *str,ll query){    ll len=strlen(str);    tree *p=&root;    for(ll i=0;i<len;i++)    {        ll id=str[i]-'0';        if(p->nex[1-id]!=0)        {            p=p->nex[1-id];        }        else        p=p->nex[id];        if(p==NULL)        return ;        if(i==len-1)        {            output=max(p->val^query,output);        }    }}int main(){    ll n;    while(~scanf("%I64d",&n))    {        output=0;        init();        memset(back,0,sizeof(back));        memset(pre,0,sizeof(pre));        memset(a,0,sizeof(a));        for(ll i=1;i<=n;i++)scanf("%I64d",&a[i]);        for(ll i=1;i<=n;i++)pre[i]=pre[i-1]^a[i],output=max(pre[i],output);        for(ll i=n;i>=1;i--)back[i]=back[i+1]^a[i],output=max(back[i],output);        for(ll i=n;i>=1;i--)        {            char s[50];            ll a=pre[i];            s[36]='\0';            for(ll j=35;j>=0;j--)            {                if(a)                {                    s[j]=a%2+'0';                    a/=2;                }                else                {                    s[j]='0';                }            }            find(s,pre[i]);            a=back[i];            for(ll j=35;j>=0;j--)            {                if(a)                {                    s[j]=a%2+'0';                    a/=2;                }                else                {                    s[j]='0';                }            }            creat(s,back[i]);        }        printf("%I64d\n",output);    }}