Codeforces Round #173 (Div. 2) E. Sausage Maximization —— Trie树 + 前缀和
来源:互联网 发布:淘宝0秒付款 编辑:程序博客网 时间:2024/06/05 00:51
题目链接:http://codeforces.com/problemset/problem/282/E
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.
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 cin, cout streams or the %I64dspecifier.
Print a single integer — the maximum pleasure BitHaval and BitAryo can get from the dinner.
21 2
3
31 2 3
3
21000 1000
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();}
- Codeforces Round #173 (Div. 2) E. Sausage Maximization —— Trie树 + 前缀和
- Sausage Maximization - CodeForces 282 E Trie树
- CodeForces Round #173 (282E) - Sausage Maximization 字典树
- Codeforces 282E Sausage Maximization Trie字典树
- Codeforces 282E Sausage Maximization (Trie)
- CF282 E Sausage Maximization[trie树]
- Codeforces 282E Sausage Maximization(字典树)
- Codeforces 282E. Sausage Maximization【trie树(非指针版)】
- Codeforces 282E(Sausage Maximization)
- CodeForces 282E Sausage Maximization
- Codeforces 282E Sausage Maximization【字典树+贪心】
- Codeforces Round #218 (Div. 2) E. Subway Innovation(思维 前缀和)
- Codeforces Round #376 (Div. 2) F. Video Cards —— 前缀和 & 后缀和
- Codeforces Round #173 (Div. 2) E
- 【Codeforces 282E】Sausage Maximization 中文题意&题解&代码(C++)
- Codeforces Round #294 (Div. 2) (D)前缀和+map
- Codeforces Round #344 (Div. 2)-A. Interview(前缀和)
- Codeforces Round #365 (Div. 2) B前缀和
- linux下的top命令参数说明 (virt,res,shr,data 的意义)
- Git提交空目录
- ProtoBuf 与 gRPC 你需要知道的知识
- PHP GD库相关图像生成和处理函数
- 原生JS自定义滚动条
- Codeforces Round #173 (Div. 2) E. Sausage Maximization —— Trie树 + 前缀和
- Java序列化与反序列化
- 第一章:tornado入门环境搭建
- 并发队列-无界阻塞优先级队列PriorityBlockingQueue原理探究
- 64位平台C/C++开发注意事项
- java多线程之join
- 从零开始:学flask 服务端API
- HTML DOM setInterval() 方法
- 数据库连接池配置详解