HDU 5536 Chip Factory ACM/ICPC 2015 Changchun(Trie)

Chip Factory

Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 3385    Accepted Submission(s): 1478

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.
1T1000
3n1000
0si109
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亚洲区长春站-重现赛(感谢东北师大)



        人生第一道Trie……

        对于异或问题,用Trie求解最大最小值是一个常用套路。本来Trie不属于我的范畴的……

        本题求一个互不相同的i、j、k,使得最后结果(si+sj)^sk最大。暴力的方法枚举三个不同的i、j、k,据说现场赛这种O(N^3)的方法居然能过?!大力出奇迹啊……

        正解的话在模拟比赛中也想到了。一开始把所有的数字加入到Trie中,然后枚举i和j,把si和sj从Tire中删除,然后在Trie找与si+sj异或结果最大的数字,并返回结果,最后再把si和sj加回Trie中。总的来说还是一道比较裸的Trie。唯一值得一提的就是那个如何找最大的数字。这个也比较显然,根据异或的法则,如果si+sj的第一位是0,那么对应的就要在Trie中往第一位为1的走,如果有,那么把对应那一位变成1,如果没有就只能往第一位为0的走,对应位置不变。就这样一直在Trie中从根往下走即可。总的时间复杂度是O(N^2log(1e9))感觉不科学,这个居然只比暴力快一半……具体见代码:

#include<bits/stdc++.h>
#define LL long long
#define INF 1e18
#define N 101000
using namespace std;

int n,a[N],ans;

struct Trie
{
    struct node{int size,ch[2];}T[N];
    int tot,root;

    void init()
    {
        tot=root=1;
        memset(T,0,sizeof(T));
    }

    void ins(int x)
    {
        int o=root;
        T[o].size++;
        for(int k=30;k>=0;k--)
        {
            int c=x&(1<<k)?1:0;
            if(!T[o].ch[c]) T[o].ch[c]=++tot;
            o=T[o].ch[c];
            T[o].size++;
        }
    }

    void del(int x)
    {
        int o=root;
        T[o].size--;
        for(int k=30;k>=0;k--)
        {
            int c=x&(1<<k)?1:0;
            o=T[o].ch[c];
            T[o].size--;
        }
    }

    int query(int x)
    {
        int o=root;
        for(int k=30;k>=0;k--)
        {
            int c=x&(1<<k)?1:0;
            if (c)				//如果当前位是1
            {
                if (T[o].ch[c^1]&&T[T[o].ch[c^1]].size) o=T[o].ch[c^1];			//往当前位是0的走
                                           else o=T[o].ch[c],x^=(1<<k);			//没有就只能往1走,当前位变为0
            } else				//否则当前位是0
            {
                if (T[o].ch[c^1]&&T[T[o].ch[c^1]].size) o=T[o].ch[c^1],x^=(1<<k);	//往当前位是1的走,如果能走就把当前为变成1
                                                   else o=T[o].ch[c];			//没有就只能往0走,当前位不变
            }

        }
        return x;
    }
} Trie;



int main()
{
    int T_T;
    cin>>T_T;
    while(T_T--)
    {
        ans=0;
        Trie.init();
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            Trie.ins(a[i]);
        }
        for(int i=1;i<=n;i++)
        {
            Trie.del(a[i]);				//插入删除ai放在外面,一个小优化
            for(int j=i+1;j<=n;j++)
            {
                Trie.del(a[j]);
                ans=max(Trie.query(a[i]+a[j]),ans);
                Trie.ins(a[j]);
            }
            Trie.ins(a[i]);
        }
        printf("%d\n",ans);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值