Codeforces 282E Sausage Maximization(字典树)
题目链接:282E Sausage Maximization
题目大意:给定一个序列A。要求从中选取一个前缀,一个后缀,能够为空,当时不能重叠。亦或和最大。
解题思路:预处理出前缀后缀亦或和,然后在字典树中维护。每次加入并查询。过程中维护ans。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
struct Tire {
int sz, g[maxn * 100][2];
void init();
void insert(ll s);
ll find(ll s);
}T;
int N;
ll A[maxn], prefix[maxn], suffix[maxn];
int main () {
scanf("%d", &N);
for (int i = 1; i <= N; i++)
scanf("%lld", &A[i]);
for (int i = 1; i <= N; i++)
prefix[i] = prefix[i-1] ^ A[i];
for (int i = N; i; i--)
suffix[i] = suffix[i+1] ^ A[i];
ll ans = 0;
T.init();
for (int i = N; i >= 0; i--) {
T.insert(suffix[i+1]);
ans = max(ans, T.find(prefix[i]));
}
printf("%lld\n", ans);
return 0;
}
void Tire::init() {
sz = 1;
memset(g[0], 0, sizeof(g[0]));
}
void Tire::insert(ll s) {
int u = 0;
for (int i = 60; i >= 0; i--) {
int v = (s>>i)&1;
if (g[u][v] == 0) {
memset(g[sz], 0, sizeof(g[sz]));
g[u][v] = sz++;
}
u = g[u][v];
}
}
ll Tire::find (ll s) {
int u = 0;
ll ret = 0;
for (int i = 60; i >= 0; i--) {
int v = ((s>>i)&1) ^ 1;
if (g[u][v])
ret |= (1LL<<i);
else
v = v ^ 1;
u = g[u][v];
}
return ret;
}
Codeforces 282E Sausage Maximization(字典树)的更多相关文章
- CodeForces Round #173 (282E) - Sausage Maximization 字典树
练习赛的时候这道题死活超时....想到了高位确定后..低位不能对高位产生影响..并且高位要尽可能的为1..就是想不出比较好的方法了实现... 围观大神博客..http://www.cnblogs.co ...
- Codeforces Round #173 (Div. 2) E. Sausage Maximization —— 字典树 + 前缀和
题目链接:http://codeforces.com/problemset/problem/282/E E. Sausage Maximization time limit per test 2 se ...
- codeforces 282E. Sausage Maximization Trie
题目链接 给n个数, 让你找出一个前缀和一个后缀, 它们异或完以后最大, 前缀和后缀都是异或得来的, 可以为空, 为空时按0算.前缀后缀不可覆盖. 这题好神, 竟然是Trie树... 首先将所有数的异 ...
- Codeforces 665E. Beautiful Subarrays (字典树)
题目链接:http://codeforces.com/problemset/problem/665/E (http://www.fjutacm.com/Problem.jsp?pid=2255) 题意 ...
- Choosing The Commander CodeForces - 817E (01字典树+思维)
As you might remember from the previous round, Vova is currently playing a strategic game known as R ...
- Codeforces 948D Perfect Security(字典树)
题目链接:Perfect Security 题意:给出N个数代表密码,再给出N个数代表key.现在要将key组排序,使key组和密码组的亦或所形成的组字典序最小. 题解:要使密码组里面每个数都找到能使 ...
- Codeforces 271D - Good Substrings [字典树]
传送门 D. Good Substrings time limit per test 2 seconds memory limit per test 512 megabytes input stand ...
- codeforces 706D (字典树)
题目链接:http://codeforces.com/problemset/problem/706/D 题意:q次操作,可以向多重集中增添,删除,询问异或最大值. 思路:转化为二进制用字典树存储,数字 ...
- Codeforces Round #311 (Div. 2) E. Ann and Half-Palindrome 字典树/半回文串
E. Ann and Half-Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...
随机推荐
- MySQL中关于日期、时间的数据类型和函数
一.日期相关的数据类型 1.datetime 占用8字节,既显示了日期,又显示了时间.其表示的日期范围为“1000-01-01 00:00:00”到“9999-12-31 23:59:59” 2.da ...
- iOS开发- 获取精确剩余电量
[UIDevice currentDevice].batteryMonitoringEnabled = YES; double deviceLevel = [UIDevice currentDevic ...
- Actor::updateMassFromShapes
unity报错Actor::updateMassFromShapes: Compute mesh inertia tensor failed for one of the actor's mesh s ...
- 【原创】Android 系统稳定性 - ANR(二)
文章都为原创,转载请注明出处,未经允许而盗用者追究法律责任. 很久之前写的了,留着有点浪费,共享之.编写者:李文栋P.S. OpenOffice粘贴过来后格式有些混乱. 1.2 如何分析ANR问题 引 ...
- c# winform 弹出确认消息框判断是否删除?
if (MessageBox.Show("确认删除?", "此删除不可恢复", MessageBoxButtons.YesNo) == DialogResult ...
- Aizu 1335 Eequal sum sets
Let us consider sets of positive integers less than or equal to n. Note that all elements of a set a ...
- Qt学习之路:自定义Model三篇,自定义委托等等
http://devbean.blog.51cto.com/448512/d-8/p-2
- svn回滚版本2
svn 版本回滚 取消对代码的修改分为两种情况: 第一种情况:改动没有被提交(commit). 这种情况下,使用svn revert就能取消之前的修改. svn revert用法如下: # svn ...
- 设计模式(二)单件模式Singleton(创建型)
SINGLETON(单件)—对象创建型模式 几乎所有面向对象的程序中,总有一些类的对象需要是唯一的,例如,通过数据库句柄到数据库的连接是独占的.您希望在应用程序中共享数据库句柄,因为在保持连接打开或关 ...
- 基于visual Studio2013解决算法导论之010快排中应用插入排序
题目 快排中引用插入排序 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <malloc.h> ...