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

题目链接: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
2
1 2
output
3
input
3
1 2 3
output
3
input
2
1000 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

 


代码如下:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const double eps = 1e-6;
 5 const int INF = 2e9;
 6 const LL LNF = 9e18;
 7 const int mod = 1e9+7;
 8 const int maxn = 1e5+10;
 9 
10 typedef struct node
11 {
12     struct node *next[2];
13 }Node, *Trie;
14 
15 Trie T;
16 LL n, a[maxn], pre[maxn], rev[maxn];
17 
18 void init()
19 {
20     scanf("%I64d",&n);
21     for(int i = 1; i<=n; i++)
22         scanf("%I64d",&a[i]);
23     for(int i = 1; i<=n; i++)   //前缀
24         pre[i] = pre[i-1]^a[i];
25     for(int i = n; i>=1; i--)   //后缀
26         rev[i] = rev[i+1]^a[i];
27 
28     T = new Node;   //初始化Trie树
29     T->next[0] = T->next[1] = NULL;
30 }
31 
32 void add(LL x)
33 {
34     Trie p = T;
35     for(int i = 50; i>=0; i--)  //从高位到低位
36     {
37         int d = (x>>i)&1;
38         if(p->next[d]==NULL)    //此位的d不存在, 则新建
39             p->next[d] = new Node, p->next[d]->next[0] = p->next[d]->next[1] = NULL;
40         p = p->next[d];
41     }
42 }
43 
44 LL query(LL x)
45 {
46     LL tmp = 0;
47     Trie p = T;
48     for(int i = 50; i>=0; i--)
49     {
50         //如果!d路存在,则可加上(d ^ !d = 1), 并顺着这条路走下去; 否则走d路(!d路和d路至少一路存在)
51         int d = (x>>i)&1;
52         if(p->next[!d]) tmp += 1LL*(1LL<<i), p = p->next[!d];
53         else p = p->next[d];
54     }
55     return tmp;
56 }
57 
58 void solve()
59 {
60     LL ans = 0;
61     for(int i = 0; i<=n; i++) //i为0时, 没有前缀;i为n时,没有后缀。
62     {
63         add(pre[i]);
64         ans = max( ans, query(rev[i+1]) );
65     }
66     printf("%I64d\n",ans);
67 }
68 
69 int main()
70 {
71     init();
72     solve();
73 }
View Code

 

转载于:https://www.cnblogs.com/DOLFAMINGO/p/7538668.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值