hdu5536 Chip Factory (01字典树删除操作)
来源:互联网 发布:用友软件联系方式 编辑:程序博客网 时间:2024/05/29 15:08
Problem Description
John is a manager of a CPU chip factory, the factory produces lots of chips everyday. To manage large amounts of products, every processor has a serial number. More specifically, the factory produces n chips today, the i-th chip produced this day has a serial number si.
At the end of the day, he packages all the chips produced this day, and send it to wholesalers. More specially, he writes a checksum number on the package, this checksum is defined as below:
which i,j,k are three different integers between 1 and n. And ⊕ is symbol of bitwise XOR.
Can you help John calculate the checksum number of today?
Input
The first line of input contains an integer T indicating the total number of test cases.
The first line of each test case is an integer n, indicating the number of chips produced today. The next line has n integers s1,s2,..,sn, separated with single space, indicating serial number of each chip.
1≤T≤1000
3≤n≤1000
0≤si≤109
There are at most 10 testcases with n>100
Output
For each test case, please output an integer indicating the checksum number in a line.
Sample Input
2
3
1 2 3
3
100 200 300
Sample Output
6
400
大致题意:给你n个数,让你从中选择三个不同的数,将其中两个数相加后异或上第三个数,使得结果最大,问最大结果为多少。
思路:先将这n个数建立成一颗01字典树,然后O(n^2)的去枚举两个不同的数,将其从字典树中删除,然后再查询此时异或最大值,然后再向字典树中加入这两个数。
代码如下
1.数组
#include<iostream>#include<cstdio>#include<algorithm> #include<cstring>using namespace std;#define LL long long #define ULL unsigned long long const int MAXN=32*1005;struct Trie{ int ch[2]; int size;}T[MAXN];int root=1,tot=1;void add(int x){ int p=root; T[p].size++; for(int i=31;i>=0;i--) { int k=(x>>i)&1; if(!T[p].ch[k]) T[p].ch[k]=++tot; p=T[p].ch[k]; T[p].size++; }}void Delete(int x){ int p=root; T[p].size--; for(int i=31;i>=0;i--) { int k=(x>>i)&1; p=T[p].ch[k]; T[p].size--; }}int query(int x){ int ans=0; int p=root; for(int i=31;i>=0;i--) { int k=(x>>i)&1; if(T[p].ch[k^1]&&T[T[p].ch[k^1]].size) ans^=(1<<i),p=T[p].ch[k^1]; else p=T[p].ch[k]; } return ans;}void init(){ for(int i=1;i<=tot;i++) T[i].ch[0]=T[i].ch[1]=T[i].size=0; tot=1;}int a[1005];int main() { int T; scanf("%d",&T); int n,num; for(int cas=1;cas<=T;cas++) { init(); int ans=-1; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); add(a[i]); } for(int i=1;i<=n;i++) { Delete(a[i]); for(int j=i+1;j<=n;j++) { Delete(a[j]); ans=max(ans,query(a[i]+a[j])); add(a[j]); } add(a[i]); } printf("%d\n",ans); } return 0;}
2.指针
#include<iostream>#include<cstdio>#include<algorithm> #include<cstring>using namespace std;#define LL long long #define ULL unsigned long long struct node{ int size; node *Next[2];};void add(node *head,int num){ node *p=head; p->size++; for(int i=31;i>=0;i--) { int k=(num>>i)&1; if(p->Next[k]==NULL) { node *q=new node(); p->Next[k]=q; } p=p->Next[k]; p->size++; }}void Delete(node *head,int num){ node *p=head; p->size--; for(int i=31;i>=0;i--) { int k=(num>>i)&1; p=p->Next[k]; p->size--; }}int query(node *head,int num){ node *p=head; int x=0; for(int i=31;i>=0;i--) { int k=(num>>i)&1; if(p->Next[k^1]&&p->Next[k^1]->size) p=p->Next[k^1],x^=(1<<i); else p=p->Next[k]; } return x;}int a[1005];int main() { int T; scanf("%d",&T); int n,num; for(int cas=1;cas<=T;cas++) { node *head=new node(); int ans=-1; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); add(head,a[i]); } for(int i=1;i<=n;i++) { Delete(head,a[i]); for(int j=i+1;j<=n;j++) { Delete(head,a[j]); ans=max(ans,query(head,a[i]+a[j])); add(head,a[j]); } add(head,a[i]); } printf("%d\n",ans); } return 0;}
- hdu5536 Chip Factory (01字典树删除操作)
- HDU5536 Chip Factory(01字典树)
- HDU5536 chip factory 【01字典树】
- HDU5536:Chip Factory(字典树)
- HDU5536 Chip Factory Trie(01字典树)
- HDU5536 Chip Factory(字典树)
- HDU5536 Chip Factory 【字典树】
- HDU5536 Chip Factory(trie树)
- hdu5536 Chip Factory(Tire)
- hdu 5536 Chip Factory(01字典树带删除操作)
- 【HDU5536 2015长春赛区 J】【字典树or暴力】Chip Factory 字典序做法+暴力做法
- HDU5536 Chip Factory(Trie树,二进制)
- hdu5536 Chip Factory (异或最大值)
- hdu5536 Chip Factory
- HDU 5536 Chip Factory 带删除的01字典树
- Chip Factory(字典树,学会将路暂时删除)
- hdu5536 Chip Factory (暴力+优化)
- HDU 5536 Chip Factory(暴力 or 01字典树)
- 封装mvc
- wondows下安装matplotlib(python包)
- 浅析printf和scanf的用法与返回值
- 从NSURLSession到AFNetworking再到自己封装一个网络框架
- 数据结构(最小生成树)
- hdu5536 Chip Factory (01字典树删除操作)
- 【备忘】Java菜鸟到大牛学习路线之实战篇
- java 自我知识总结 (十)构造函数特点
- 背包问题???
- eclipse+MinGW fatal error: stdlib.h: No such file or directory 问题
- 51nod 1003 阶乘后面0的数量
- com.android.ddmlib.InstallException: Failed to install all
- KMP算法
- 12个scp传输文件的命令栗子