CSU 1216: 异或最大值(字典树+贪心)
2016-09-09 22:41
316 查看
1216: 异或最大值
Time Limit: 2 Sec Memory Limit: 128 MB
Description
给定一些数,求这些数中两个数的异或值最大的那个值
Input
第一行为数字个数n,1 <= n <= 10 ^ 5。接下来n行每行一个32位有符号非负整数。
Output
任意两数最大异或值
Sample Input
3
3
7
9
Sample Output
14
题解:
对每一个数的二进制直接建立字典树,然后贪心即可。
Time Limit: 2 Sec Memory Limit: 128 MB
Description
给定一些数,求这些数中两个数的异或值最大的那个值
Input
第一行为数字个数n,1 <= n <= 10 ^ 5。接下来n行每行一个32位有符号非负整数。
Output
任意两数最大异或值
Sample Input
3
3
7
9
Sample Output
14
题解:
对每一个数的二进制直接建立字典树,然后贪心即可。
#include<bits/stdc++.h> using namespace std; const int maxn=1e5+5; struct note { struct note *Next[2]; int Data; note() { Next[0]=Next[1]=NULL; } }; int Bit[31],S,ans,val; void Insert(struct note *&r) { note *p=r; for(int i=30; i>=0; --i) { if(p->Next[Bit[i]]==NULL) { note *temp=new note(); p->Next[Bit[i]]=temp; } p=p->Next[Bit[i]]; } } int Query(struct note *&r) { struct note *p=r; int tans=0; for(int i=30; i>=0; --i) { if(p->Next[Bit[i]^1]!=NULL){ tans|=(1<<i),p=p->Next[Bit[i]^1]; }else if(p->Next[Bit[i]]!=NULL){ p=p->Next[Bit[i]]; }else return tans; } return tans; } void reSolve(){ S=0; while(val){ Bit[S++]=val&1; val>>=1; } while(S<=30){ Bit[S++]=0; } } int main() { int n; while(scanf("%d",&n)==1) { struct note *Root=new note(); ans=0; for(int i=0;i<n;++i){ scanf("%d",&val); reSolve(); Insert(Root); ans=max(ans,Query(Root)); } printf("%d\n",ans); } return 0; }
相关文章推荐
- ACM学习历程—CSU 1216 异或最大值(xor && 贪心 && 字典树)
- CSU 1216 异或最大值【字典树】
- CSU-1216: 异或最大值-trie-01字典树
- CSU 1216 —— 异或最大值
- CSUOJ 1216 异或最大值 (01Trie)
- CSU_1216(异或最大值)
- csu-1216: 异或最大值
- CSU 1216 异或最大值 01trie
- CSU_1216【异或最大值】
- CSU 1216 异或最大值
- COJ 1216: 异或最大值(01字典树)
- CSU 1216 异或最大值(01Trie)
- CSU 1216 异或最大值
- bzoj3261 最大异或和(可持久化字典树)
- HDU 5536 Chip Factory(01字典树找最大异或值)——2015ACM/ICPC亚洲区长春站
- 中南大学COJ 1216: 异或最大值(数据结构)
- COJ 1216 最大异或值
- 【BZOJ 4571】美味 【区间异或最大值】【主席树】【贪心】
- POJ 3764 The xor-longest( 树上异或前缀和&字典树求最大异或)
- COJ 1216 异或最大值