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\n",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\n",ans);
33     }
34     return  0;
35 }

 

转载于:https://www.cnblogs.com/tingtin/p/9628046.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值