ACM Changchun 2015 J. Chip Factory

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 nn 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:

display  maxi,j,k(si+sj)sk

whichi,j,k are three different integers between 1 and n. And oplus⊕ is symbol of bitwise XOR.

Can you help John calculate the checksum number of today?

Input Format

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 nn integers s1,s2,..,sn, separated with single space, indicating serial number of each chip.

1T1000

3n1000

0si109

• There are at most 10 testcases with n>100

Output Format

For each test case, please output an integer indicating the checksum number in a line.

样例输入

2
3
1 2 3
3
100 200 300

样例输出

6
400

题目来源

ACM Changchun 2015

 

  1 #include <iostream>
  2 #include <algorithm>
  3 #include <cstring>
  4 #include <cstdio>
  5 #include <vector>
  6 #include <queue>
  7 #include <stack>
  8 #include <cstdlib>
  9 #include <iomanip>
 10 #include <cmath>
 11 #include <cassert>
 12 #include <ctime>
 13 #include <map>
 14 #include <set>
 15 using namespace std;
 16 #define  ull unsigned  long  long 
 17 #define ll  long  long 
 18 #define  N  30009
 19 int num[N],tree[N][2];
 20 int t,n,a[1100];
 21 int pos;
 22 //01字典树
 23 void  insert(int x,int rt)
 24 {
 25     for(int i=30;i>=0;i--)
 26     {
 27         int y=0;
 28         if(x>>i&1)  y=1;
 29         if(!tree[rt][y])  tree[rt][y]=pos++;         
 30         rt=tree[rt][y];
 31         num[rt]++;
 32     }
 33 }
 34 void  dele(int x,int rt)
 35 {
 36     for(int i=30;i>=0;i--)
 37     {
 38         int y=0;
 39         if(x>>i&1) y=1;
 40         rt=tree[rt][y];
 41         num[rt]--;//tree[rt][y] 还在,并不是真正的删除
 42     }
 43 }
 44 int solve(int x,int rt)
 45 {
 46     int ret=0;
 47     /*
 48     00001010
 49     10100100是倒着来的
 50     那么只要字典树里首位有1,就不可能找到10100100
 51     */
 52     for(int i=30;i>=0;i--)//一定要倒过来,因为贪心,高位大,最终的结果才大
 53     {
 54         int y=0;
 55         if(x>>i&1) y=1;
 56         if(tree[rt][y^1]&&num[tree[rt][y^1]])//num[tree[rt][y^1]]才有意义  {
 57             rt=tree[rt][y^1];
 58             ret+=(1<<i);//该位的异或为1
 59         }
 60         else{
 61             rt=tree[rt][y];
 62         }
 63     }
 64     return ret;
 65 }
 66 int  main()
 67 {
 68     scanf("%d",&t);
 69     while(t--)
 70     {
 71         for(int i=0;i<N;i++)
 72         {
 73             num[i]=0;
 74             for(int j=0;j<=1;j++)
 75             {
 76                 tree[i][j]=0;
 77             }
 78         }
 79         pos=1;
 80         scanf("%d",&n);
 81         for(int i=0;i<n;i++){
 82             scanf("%d",&a[i]);
 83             insert(a[i],0);
 84         }
 85         int ans=0;
 86         //每次要删除a[i],a[j] 因为i!=j!=k
 87         //当然每次查询后,还要再次插入字典树
 88         for(int i=0;i<n;i++)
 89         {    dele(a[i],0);
 90             for(int j=i+1;j<n;j++)
 91             {
 92                 dele(a[j],0);
 93                 ans=max(ans,solve(a[i]+a[j],0) );
 94                 insert(a[j],0);
 95             }
 96             insert(a[i],0);
 97         }
 98         printf("%d
",ans);
 99     }
100     return 0;
101 }
 1 //9s  的暴力解法
 2 #define  N  1009
 3 int t,n,a[N];
 4 int ans;
 5 int solve(int x,int y,int z)
 6 {
 7     return (x+y)^z;
 8 }
 9 int main()
10 {
11     scanf("%d",&t);
12     while(t--)
13     {
14         scanf("%d",&n);
15         for(int i=0;i<n;i++)
16         {
17             scanf("%d",&a[i]);
18         }
19         int ans=0;
20         for(int i=0;i<n;i++)
21         {
22             for(int j=i+1;j<n;j++)
23             {
24                 for(int k=j+1;k<n;k++)
25                 {
26                     ans=max(ans,solve(a[i],a[j],a[k]) );
27                     ans=max(ans,solve(a[i],a[k],a[j]) );
28                     ans=max(ans,solve(a[k],a[j],a[i]) );
29                 }
30             }
31         }
32         printf("%d
",ans);
33     }
34     return  0;
35 }
原文地址:https://www.cnblogs.com/tingtin/p/9628046.html