Codeforces 282E Sausage Maximization【字典树+贪心】
来源:互联网 发布:linux 设置目录权限 编辑:程序博客网 时间:2024/05/22 00:28
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 (thexor 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 thecin,cout streams or the%I64d specifier.
Print a single integer — the maximum pleasure BitHaval and BitAryo can get from the dinner.
21 2
3
31 2 3
3
21000 1000
1000
题目大意:
给一个长度为n的整数序列,现在要你截取这个序列的一个前缀和一个后缀(前缀和后缀不能相交),使得前缀和后缀的异或值最大。
思路:
贪心去做,对于每个查询,同样处理成35位二进制01字符串,对应进行查询,如果当前位子是0,那么尽量往1那边走,同理,如果当前位子是1,那么尽量往0那边走即可。
那么O(n)建树+查询即可。
Ac代码:
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<iostream>using namespace std;#define ll __int64ll a[100002];ll pre[100002];ll back[100002];#define maxn 2typedef struct tree{ tree *nex[maxn]; ll v; ll val;}tree;ll output;tree root;void init(){ for(ll i=0;i<maxn;i++) { root.nex[i]=NULL; }}void creat(char *str,ll va){ ll len=strlen(str); tree *p=&root,*q; for(ll i=0;i<len;i++) { ll id=str[i]-'0'; if(p->nex[id]==NULL) { q=(tree *)malloc(sizeof(root)); q->v=1; for(ll j=0;j<2;j++) { q->nex[j]=NULL; } p->nex[id]=q; } else { p->nex[id]->v++; } p=p->nex[id]; if(i==len-1) { p->val=va; } }}void find(char *str,ll query){ ll len=strlen(str); tree *p=&root; for(ll i=0;i<len;i++) { ll id=str[i]-'0'; if(p->nex[1-id]!=0) { p=p->nex[1-id]; } else p=p->nex[id]; if(p==NULL) return ; if(i==len-1) { output=max(p->val^query,output); } }}int main(){ ll n; while(~scanf("%I64d",&n)) { output=0; init(); memset(back,0,sizeof(back)); memset(pre,0,sizeof(pre)); memset(a,0,sizeof(a)); for(ll i=1;i<=n;i++)scanf("%I64d",&a[i]); for(ll i=1;i<=n;i++)pre[i]=pre[i-1]^a[i],output=max(pre[i],output); for(ll i=n;i>=1;i--)back[i]=back[i+1]^a[i],output=max(back[i],output); for(ll i=n;i>=1;i--) { char s[50]; ll a=pre[i]; s[36]='\0'; for(ll j=35;j>=0;j--) { if(a) { s[j]=a%2+'0'; a/=2; } else { s[j]='0'; } } find(s,pre[i]); a=back[i]; for(ll j=35;j>=0;j--) { if(a) { s[j]=a%2+'0'; a/=2; } else { s[j]='0'; } } creat(s,back[i]); } printf("%I64d\n",output); }}
- Codeforces 282E Sausage Maximization【字典树+贪心】
- Codeforces 282E Sausage Maximization(字典树)
- CodeForces Round #173 (282E) - Sausage Maximization 字典树
- Codeforces 282E Sausage Maximization Trie字典树
- Sausage Maximization - CodeForces 282 E Trie树
- Codeforces 282E(Sausage Maximization)
- CodeForces 282E Sausage Maximization
- Codeforces 282E Sausage Maximization (Trie)
- Codeforces 282E. Sausage Maximization【trie树(非指针版)】
- CF282 E Sausage Maximization[trie树]
- 【Codeforces 282E】Sausage Maximization 中文题意&题解&代码(C++)
- Codeforces Round #173 (Div. 2) E. Sausage Maximization —— Trie树 + 前缀和
- codeforces 665E (字典树)
- codeforces 817E 字典树计数
- Codeforces 706d 字典树+贪心
- codeforces 732E (贪心)
- Codeforces 777E 贪心
- codeforces 777E 贪心
- Shell脚本:人机交互脚本-scp
- python调用Face++,玩坏了!
- eclipse中java项目的build path详解
- UDT源码剖析(四):UDT的GC线程相关过程代码注释
- [LeetCode 解题报告]011.Container With Most Water
- Codeforces 282E Sausage Maximization【字典树+贪心】
- HBase 常用Shell命令
- Spark在不同集群中的运行架构
- MySQL安装applying security settings错误解决方案
- MongoDB
- OC动画2(头尾式动画)
- springboot基础环境搭建
- MAVEN项目的一些细节
- 二、MyEclipse 安装 GRADLE