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 ...
随机推荐
- 把Execl表格中的数据获取出来保存到数据库中
比如我们遇到一些需要把execl表格中的数据保存到数据库中,一条一条保存效率底下而且容易出错,数据量少还好,一旦遇到数据量大的时候就会累死个人啊,下面我们就来把execl表格中数据保存到对应的数据库中 ...
- BZOJ3295动态逆序对
一道比较傻的CDQ分治 CDQ: 主要用于解决三位偏序的问题 #include<cstdio> #include<cctype> #include<algorithm&g ...
- Java创建和解析Json数据方法(三)——json-lib包的使用
(三)json-lib包的使用 这篇笔记主要介绍json-lib包的创建和解析json数据的方式,主要是的JSONObject.JSONArray和Java对象:beans, maps ...
- Java过滤HTML标签工具类
过滤HTML标签能有效的放置XSS攻击. 封装: import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springf ...
- SQL Server 监控系列 —— 二
http://www.cnblogs.com/bhtfg538/archive/2011/01/21/1939706.html
- linux 和网络安装 linux
接触linux 一年了,一直没有总结过什么东西.在开学前的这么几天把之前做的一些东西重新整理一下吧. 或许还会有别的收获呢. linux安装: 去年10月份接触linux以来安装不下数十次,不管是光盘 ...
- Delphi图像处理 -- 颜色矩阵变换
转载自阿发伯:http://blog.csdn.net/maozefa/article/details/8316430 阅读提示: <Delphi图像处理>系列以效率为侧重点,一般 ...
- Android GC 原理探究
导语 想写一篇关于 android GC 的想法来源于追查一个魅族手机图片滑动卡顿问题,由于不断的 GC 导致的丢帧卡顿的问题让我们想了很多方案去解决,所以就打算详细的看看内存分配和 GC 的原理,为 ...
- HDU 2236 无题II(二分图匹配+二分)
HDU 2236 无题II 题目链接 思路:行列仅仅能一个,想到二分图,然后二分区间长度,枚举下限.就能求出哪些边是能用的,然后建图跑二分图,假设最大匹配等于n就是符合的 代码: #include & ...
- C#应用程序配置文件.config介绍
我们经常会希望在程序中写入一些配置信息,例如版本号,以及数据库的连接字符串等.你可能知道在WinForm应用程序中可以利用Properties.Settings来进行类似的工作,但这些其实都利用了Ap ...