cf-282e
“字典树”的变形,任意两数异或最大值,处理字典树的时候可以用递归,也可以用循环,下面有两个版本。
C - Sausage Maximization
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64uSubmit Status
Practice CodeForces 282E
Description
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.
Input
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.
Output
Print a single integer — the maximum pleasure BitHaval and BitAryo can get from the dinner.
Sample Input
Input
21 2
Output
3
Input
31 2 3
Output
3
Input
21000 1000
Output
1000
#include <stdio.h>
#include <algorithm>
using namespace std;
typedef __int64 LL;
const int maxn = +;
int ch[maxn*][], cnt, root, n;
LL num[maxn], ans;
void Insert(LL tar)
{
int cur = root;
for(int i = ; i >= ; i--)
{
int id = ((tar & (1LL<<(i-))) ? : );
if(ch[cur][id] == -)
{
ch[cnt][] = ch[cnt][] = -;
ch[cur][id] = cnt++;
}
cur = ch[cur][id];
}
}
void Find(LL tar)
{
int cur = root;
LL ret = ;
for(int i = ; i >= ; i--)
{
int id = ((tar & (1LL<<(i-))) ? : );
if(ch[cur][id^] != -)
{
ret |= (1LL << (i-));
cur = ch[cur][id^];
}
else
cur = ch[cur][id];
}
ans = max(ans, ret);
}
int main()
{
LL pre, suf;
while(scanf("%d", &n) != EOF)
{
pre = suf = cnt = ;
ch[cnt][] = ch[cnt][] = -;
root = cnt++;
Insert(0LL);
for(int i = ; i < n; i++)
{
scanf("%I64d", &num[i]);
suf ^= num[i];
}
ans = suf;
for(int i = ; i < n; i++)
{
pre ^= num[i];
suf ^= num[i];
Insert(pre);
Find(suf);
}
printf("%I64d\n", ans);
}
return ;
}
/*
#include <stdio.h>
#include <algorithm>
using namespace std;
typedef __int64 LL;
const int maxn = 100000+100;
int ch[maxn*40][2], cnt, root, n;
LL num[maxn], ans;
void Insert(LL tar)
{
int cur = root;
for(int i = 40; i >= 1; i--)
{
int id = ((tar & (1LL<<(i-1))) ? 1 : 0);
if(ch[cur][id] == -1)
{
ch[cnt][0] = ch[cnt][1] = -1;
ch[cur][id] = cnt++;
}
cur = ch[cur][id];
}
}
void Find(LL tar)
{
int cur = root;
LL ret = 0;
for(int i = 40; i >= 1; i--)
{
int id = ((tar & (1LL<<(i-1))) ? 1 : 0);
if(ch[cur][id^1] != -1)
{
ret |= (1LL << (i-1));
cur = ch[cur][id^1];
}
else
cur = ch[cur][id];
}
ans = max(ans, ret);
}
int main()
{
LL pre, suf;
while(scanf("%d", &n) != EOF)
{
pre = suf = cnt = 0;
ch[cnt][0] = ch[cnt][1] = -1;
root = cnt++;
Insert(0LL);
for(int i = 0; i < n; i++)
{
scanf("%I64d", &num[i]);
suf ^= num[i];
}
ans = suf;
for(int i = 0; i < n; i++)
{
pre ^= num[i];
suf ^= num[i];
Insert(pre);
Find(suf);
}
printf("%I64d\n", ans);
}
return 0;
}
#include <stdio.h> typedef __int64 LL;
const int maxn = + ;
int memory[maxn*][], allocp, root, n;
LL num[maxn], ans;
void Insert(LL tar)
{
LL cur = root;
for(int i = ; i >= ; i--) {
int k = (((1LL<<i) & tar)?:);
if(memory[cur][k] == -) {
memory[allocp][] = memory[allocp][] = -;
memory[cur][k] = allocp++;
}
cur = memory[cur][k];
}
}
void Find(LL x)
{
LL ret = ;
int cur = root;
for(int i = ; i >= ; i--) {
int k = (x & (1LL<<i))?:;
if(memory[cur][k^] != -) {
ret |= (1LL << i);
cur = memory[cur][k^];
} else {
cur = memory[cur][k];
}
}
ans = (ans>ret)?ans:ret;
}
int main()
{
LL pre, suf;
while(~scanf("%d", &n)) {
pre = suf = allocp = ;
memory[allocp][] = memory[allocp][] = -;
root = allocp++;
Insert(0LL);
for(LL i = ; i < n; i++) {
scanf("%I64d", &num[i]);
suf ^= num[i];
}
ans = suf;
for(int i = ; i < n; i++)
{
pre ^= num[i];
suf ^= num[i];
Insert(pre);
Find(suf);
}
printf("%I64d\n", ans);
}
return ;
}
cf-282e的更多相关文章
- ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'
凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- cf Round 613
A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...
- ARC下OC对象和CF对象之间的桥接(bridge)
在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...
- [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现
1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...
- CF memsql Start[c]UP 2.0 A
CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...
- CF memsql Start[c]UP 2.0 B
CF memsql Start[c]UP 2.0 B B. Distributed Join time limit per test 1 second memory limit per test 25 ...
- CF #376 (Div. 2) C. dfs
1.CF #376 (Div. 2) C. Socks dfs 2.题意:给袜子上色,使n天左右脚袜子都同样颜色. 3.总结:一开始用链表存图,一直TLE test 6 (1)如果需 ...
- CF #375 (Div. 2) D. bfs
1.CF #375 (Div. 2) D. Lakes in Berland 2.总结:麻烦的bfs,但其实很水.. 3.题意:n*m的陆地与水泽,水泽在边界表示连通海洋.最后要剩k个湖,总要填掉多 ...
- CF #374 (Div. 2) D. 贪心,优先队列或set
1.CF #374 (Div. 2) D. Maxim and Array 2.总结:按绝对值最小贪心下去即可 3.题意:对n个数进行+x或-x的k次操作,要使操作之后的n个数乘积最小. (1)优 ...
随机推荐
- win7:Remote Desktop Services 启动失败
背景: 其他PC使用mstsc远程某win7 pro sp1,一直失败. 分析: 影响远程桌面应用的设置有两个: 1. 计算机远程设置中,启用“允许远程协助连接这台计算机”,且远程桌面设置正确.如选择 ...
- An unknown Subversion error occurred. (code = 155037)
这是因为在svn更新时意外中断引起的. 我的解决办法:如果本地没有更改,只是单纯获取svn的项目,则另起一个文件夹,重新checkout: 如果是本地有更改,则复制到新的文件夹,重新update.
- JMeter学习-013-JMeter 逻辑控制器之-如果(If)控制器
前文简述了 JMeter 如何通过 HTTP Cookie管理器,实现了在不执行登录操作的情况下,通过 Cookie 实现登录态的操作,具体请参阅:JMeter学习-012-JMeter 配置元件之- ...
- IN和exists 之间的比较
IN和exists 之间的比较 NOT IN 和 NOT EXISTS之间的比较
- 破解win10系统密码
- MOGRE学习笔记(2) - MOGRE基础知识总结
前一篇配置了MOGRE的运行环境,这里记录一些MOGRE基础知识,仅仅是最基础的一些东西.由于本人接触ogre的时间比较短,对于很多知识都是一知半解,所以理解起来不免会有一些错误,本人也希望自己在对o ...
- Linux权限值问题
0660:从左向右:第一位:(我不清楚,也没有用过)第二位:当前用户的经权限:6=110(二进制),每一位分别对就 可读,可写,可执行,,6说明当前用户可读可写不可执行第三位:group组用户,6的意 ...
- hbase基本操作
public class Demo { private Configuration conf; private Connection conn; @Before public void prepare ...
- CString + UINT Error:有多个运算符"+="与这些操作数匹配
在OnChar中,参数UINT nChar 有一个CString str,现在执行 str += nChar报错:Error:有多个运算符"+="与这些操作数匹配 解决办法:把UI ...
- 修改了系统自带头文件后,Xcode会报错
1.Xcode自带头文件的路径 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Develo ...