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 ...
随机推荐
- 小W摆石子
可以确定, 最后围成是 一个长方形 + 多出一列 的形状. 而且多出的那一列应该是和较短的边相邻. 贴代码. #include<iostream> #include<algorith ...
- 邁向IT專家成功之路的三十則鐵律 鐵律二十三:IT人的成家之道-樸實
根據內政部一份2013年最新的調查報告指出台灣人的離婚率位居全球第三,想想看如果這是經濟成長率的排名表現那該有多好.然而究竟為何在台灣這塊小小的土地上,不僅離婚非常高而且晚婚的人也非常的多,其原因肯定 ...
- 【IntelliJ IDEA】idea导入项目只显示项目中的文件,不显示项目结构
导入项目之后,只显示项目文件,不显示项目结构 解决方法 1.点击file->project structure..->Modules 点击右上角+加号 ->import Module ...
- Java反射之Field用法
在Java反射中Field用于获取某个类的属性或该属性的属性值 一:如何通过Field反射获取类的属性 Field提供如下几种方法: :1:Class.getDeclaredField(String ...
- How to set the initial value of a select element using AngularJS ng-options & track by
原文: https://www.gurustop.net/blog/2014/01/28/common-problems-and-solutions-when-using-select-element ...
- hql 时间
1.hql中时间格式转换 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String d ...
- vue2.0 + vux (三)MySettings 页
1.MySettings.vue <!-- 我的设置 --> <template> <div> <img class="img_1" sr ...
- HDOJ1160 Fat Mouse's Speed
FatMouse's Speed pid=1160">http://acm.hdu.edu.cn/showproblem.php?pid=1160 最长递增子序列问题的一个变体.实际上 ...
- python的多线程问题
在对文件进行预处理的时候,由于有的文件有太大,处理很慢,用python处理是先分割文件,然后每个文件起一个线程处理,启了10个线程,结果还比不起线程慢一些,改成多进程之后就好了. 使用multipro ...
- leetcode第一刷_Permutation Sequence
这道题还挺好的,假设你的思路是每次生成一个全排列,然后累计到k次,那么停下来吧.肯定超时了亲.. 微软今年的笔试题里有一道类似的,我之前已经提到过了.是唯独0和1的字符串,求第k个排列是什么样子的.这 ...