当前位置: 首页 > 工具软件 > Sausage > 使用案例 >

Sausage Maximization - CodeForces 282 E Trie树

伯英锐
2023-12-01

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.

Sample test(s)
input
2
1 2
output
3
input
3
1 2 3
output
3
input
2
1000 1000
output
1000


题意:找一个前缀和一个后缀,使其异或的结果最大。

思路:对于一个后缀,将他前面的所有的前缀加到一个Trie树中,然后根据这个后缀数每个位上的0和1,寻找使其异或最大的数。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll val[100010],pri[100010],past[100010],pow2[50],num,ans;
struct Trie
{ int index;
  Trie *next[2];
  Trie()
  { index=-1;
    memset(next,0,sizeof(next));
  }
};
Trie *root=new Trie;
void Trie_Insert(Trie *tr,ll k,int pos)
{ ll p=k%pow2[pos+1]/pow2[pos];
  if(pos>=1)
  { if(tr->next[p]==0)
     tr->next[p]=new Trie;
    Trie_Insert(tr->next[p],k,pos-1);
  }
  else
   tr->index=1;
}
void Trie_Search(Trie *tr,ll k,int pos)
{ ll p=k%pow2[pos+1]/pow2[pos];
  if(pos>0)
  { if(p==0)
    { if(tr->next[1]!=0)
      { num+=pow2[pos];
        Trie_Search(tr->next[1],k,pos-1);
      }
      else
       Trie_Search(tr->next[0],k,pos-1);
    }
    if(p==1)
    { if(tr->next[0]!=0)
       Trie_Search(tr->next[0],k,pos-1);
      else
      { num+=pow2[pos];
        Trie_Search(tr->next[1],k,pos-1);
      }
    }
  }
}
int main()
{ int n,m,i,j,k;
  scanf("%d",&n);
  pow2[0]=1;
  pow2[1]=1;
  for(i=2;i<=42;i++)
   pow2[i]=pow2[i-1]*2;
  for(i=1;i<=n;i++)
   scanf("%I64d",&val[i]);
  for(i=1;i<=n;i++)
   pri[i]=pri[i-1]^val[i];
  for(i=n;i>=1;i--)
   past[i]=past[i+1]^val[i];
  Trie_Insert(root,0,40);
  for(i=1;i<=n+1;i++)
  { num=0;
    Trie_Search(root,past[i],40);
    ans=max(ans,past[i]^num);
    Trie_Insert(root,pri[i],40);
  }
  printf("%I64d\n",ans);
}




 类似资料: