hdu5536Chip Factory
2015-11-12 13:48
471 查看
算机学院大学生程序设计竞赛(新生为主) |
Chip FactoryTime Limit: 18000/9000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 640 Accepted Submission(s): 320 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: maxi,j,k(si+sj)⊕sk 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 Source 2015ACM/ICPC亚洲区长春站-重现赛(感谢东北师大) #include<bits/stdc++.h> const int maxn=51000; const int ch_size=2; using namespace std; int n,a[maxn]; unsigned int ans; struct Trie{ int a[maxn][ch_size],tot,sum[maxn]; void init(){sum[0]=tot=0;memset(a[0],0,sizeof(a[0]));} void clear(int x){memset(a[x],0,sizeof(a[x]));sum[x]=0;} void insert(unsigned int v){ int x=0;sum[0]++; for(int i=31;i>=0;i--){ bool son=((v>>i)&1); if(a[x][son]){ x=a[x][son]; }else{ clear(++tot); x=a[x][son]=tot; } sum[x]++; } } void update(unsigned int v,unsigned int f1,unsigned int f2){ int x=0;unsigned int tmp=0; bool k1=1,k2=1; // printf("%u %u %u\n",v,f1,f2); for(int i=31;i>=0;i--){ bool son=((v>>i)&1); // printf("%d %d %d %d %d %d %d %u\n",i,son,(((f1>>i)&1)^son),(((f2>>i)&1)^son),sum[a[x][son^1]],k1,k2,1u<<i); if(a[x][son^1]&&sum[a[x][son^1]]-k1*(((f1>>i)&1)^son)-k2*(((f2>>i)&1)^son)>0){ x=a[x][son^1]; if((((f1>>i)&1)^son)==0)k1=0; if((((f2>>i)&1)^son)==0)k2=0; tmp|=(1u<<i); }else{ x=a[x][son]; if(((f1>>i)&1)!=son)k1=0; if(((f2>>i)&1)!=son)k2=0; } } ans=max(ans,tmp); // printf("tmp = %d\n",tmp); } }T; void work(){ T.init(); scanf("%d",&n); ans=0; for(int i=1;i<=n;i++){ scanf("%u",&a[i]); T.insert(a[i]); } for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++){ unsigned int t=a[i]+a[j]; T.update(t,a[i],a[j]); } printf("%u\n",ans); } int main(){ int t;scanf("%d",&t); while(t--)work(); return 0; } |
相关文章推荐
- Python Trie树实现字典排序
- Java中实现双数组Trie树实例
- POJ 1056 IMMEDIATE DECODABILITY
- trie树的一个简单例子源码
- Use Python to implement Trie
- 字符串的多模式匹配
- 数据结构之Trie树
- Longest Common Prefix
- LeetCode - Word Search II
- LeetCode - Implement Trie (Prefix Tree)
- [codevs 1187]Xor最大路径 Trie+贪心
- hdu2112HDU Today trie+map
- poj2513Colored Sticks trie+merge_set+Eular
- [Leetcode]Add and Search Word - Data structure design
- poj 1816 Trie+DFS匹配模式串
- poj 1997 trie树
- 从Trie树(字典树)谈到后缀树(10.28修订)(转载)
- 【Trie】bzoj1954——我终于当了权限狗了!
- Trie树计算单词前缀的个数
- Find the maximum subarray XOR in a given array