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 }
复制代码

 

posted on   cltt  阅读(165)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示