Codeforces Round #173 (Div. 2) E. Sausage Maximization —— 字典树 + 前缀和
题目链接:http://codeforces.com/problemset/problem/282/E
2 seconds
256 megabytes
standard input
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.
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.
2
1 2
3
3
1 2 3
3
2
1000 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-;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = 1e5+; typedef struct node
{
struct node *next[];
}Node, *Trie; Trie T;
LL n, a[maxn], pre[maxn], rev[maxn]; void init()
{
scanf("%I64d",&n);
for(int i = ; i<=n; i++)
scanf("%I64d",&a[i]);
for(int i = ; i<=n; i++) //前缀
pre[i] = pre[i-]^a[i];
for(int i = n; i>=; i--) //后缀
rev[i] = rev[i+]^a[i]; T = new Node; //初始化Trie树
T->next[] = T->next[] = NULL;
} void add(LL x)
{
Trie p = T;
for(int i = ; i>=; i--) //从高位到低位
{
int d = (x>>i)&;
if(p->next[d]==NULL) //此位的d不存在, 则新建
p->next[d] = new Node, p->next[d]->next[] = p->next[d]->next[] = NULL;
p = p->next[d];
}
} LL query(LL x)
{
LL tmp = ;
Trie p = T;
for(int i = ; i>=; i--)
{
//如果!d路存在,则可加上(d ^ !d = 1), 并顺着这条路走下去; 否则走d路(!d路和d路至少一路存在)
int d = (x>>i)&;
if(p->next[!d]) tmp += 1LL*(1LL<<i), p = p->next[!d];
else p = p->next[d];
}
return tmp;
} void solve()
{
LL ans = ;
for(int i = ; i<=n; i++) //i为0时, 没有前缀;i为n时,没有后缀。
{
add(pre[i]);
ans = max( ans, query(rev[i+]) );
}
printf("%I64d\n",ans);
} int main()
{
init();
solve();
}
Codeforces Round #173 (Div. 2) E. Sausage Maximization —— 字典树 + 前缀和的更多相关文章
- 贪心 Codeforces Round #173 (Div. 2) B. Painting Eggs
题目传送门 /* 题意:给出一种方案使得abs (A - G) <= 500,否则输出-1 贪心:每次选取使他们相差最小的,然而并没有-1:) */ #include <cstdio> ...
- Codeforces Round #173 (Div. 2)
A. Bit++ 模拟. B. Painting Eggs 贪心,每个物品给使差值较小的那个人,根据题目的约数条件,可证明贪心的正确性. C. XOR and OR \(,,00 \to 00,01 ...
- Codeforces 282E Sausage Maximization(字典树)
题目链接:282E Sausage Maximization 题目大意:给定一个序列A.要求从中选取一个前缀,一个后缀,能够为空,当时不能重叠.亦或和最大. 解题思路:预处理出前缀后缀亦或和,然后在字 ...
- Codeforces Round #603 (Div. 2) E. Editor(线段树)
链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...
- CodeForces Round #173 (282E) - Sausage Maximization 字典树
练习赛的时候这道题死活超时....想到了高位确定后..低位不能对高位产生影响..并且高位要尽可能的为1..就是想不出比较好的方法了实现... 围观大神博客..http://www.cnblogs.co ...
- Codeforces Round #329 (Div. 2) D. Happy Tree Party 树链剖分
D. Happy Tree Party Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/593/p ...
- Codeforces Round #244 (Div. 2) B. Prison Transfer 线段树rmq
B. Prison Transfer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/pro ...
- Codeforces Round #328 (Div. 2) D. Super M 虚树直径
D. Super M Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/592/problem/D ...
- Codeforces Round #274 (Div. 1) C. Riding in a Lift 前缀和优化dp
C. Riding in a Lift Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/pr ...
随机推荐
- 同源策略Same-origin policy
同源策略Same-origin policy 同源策略Same-origin policy是Web应用的一种安全基础策略.它规定同一源中,页面包含的脚本可以访问该源下的其他页面的数据.只有当网址中的 ...
- chpasswd、dd命令、find实战、添加系统服务、buffer、cached
1.如果两个文件的每一行想一一对应 paste 1.txt 2.txt # 文件3.txt中存放着用户跟密码,想要添加用户并设置密码: # 用户必须存在,文件格式必须是--用户名:密码 chpassw ...
- Android 自定义ListView Item侧滑删除
本程序是基于网上开源项目修改而来,具体来源忘了,懒得搜了,如果有不合适的地方,请原作者联系我,我会及时回复和处理的! 该例子程序中主要包含两个ListView,一个是实现侧滑删除,一个是侧滑出菜单,代 ...
- andriod Java中度转度分秒
public String trandu2m(double d) { //gisoracle 编号 try { //double dd = Convert.ToDouble(str); String ...
- gvim的常用编辑快捷键
gvim的快捷键很多,很难记全,但是入门初期应该找过几种基本的命令 下面结合自己常用到的介绍下 光标跳转: 0:行首 $:行尾 e:下一个单词的结尾 w:下一个单词的开头 b:上一个单词 H:当前页面 ...
- Oracle数据库有用函数
有用函数 DECODE 语法例如以下: DECODE(value, if1, then1, if2,then2,if3,then3, . . . else ) Value 代表某个表的不论什么类型的 ...
- Android——动画的分类
Android包含三种动画:View Animation, Drawable Animation, Property Animation(Android 3.0新引入). 1.View Animati ...
- NoSQL数据库-MongoDB和Redis
http://blog.csdn.net/tea_wu/article/details/19050277 http://www.uml.org.cn/sjjm/201212205.asp
- Solaris网络基础
划分子网: 把大网缩小为若干个小网.修改子网掩码,划分多个网络. 那么如何确定子网的子网掩码和IP地址? 以上你会发现少了6个IP. Ifconfig e1000g0 down down掉网卡 ...
- Django-celery分布式任务
昨天一个很好的面试官问我你在python中怎么实现定时任务呢?我没回答好,我问了下原来有个叫celery的东西,感觉挺好用的 Celery 是一个 基于python开发的分布式异步消息任务队列,通过它 ...