hdu5536(01字典树)

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 ii

-th chip produced this day has a serial number sisi

.

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)⊕skmaxi,j,k(si+sj)⊕sk

which i,j,ki,j,k

are three different integers between 11

and nn

. And ⊕⊕

is symbol of bitwise XOR.

Can you help John calculate the checksum number of today? InputThe first line of input contains an integer TT

indicating the total number of test cases.

The first line of each test case is an integer nn

, indicating the number of chips produced today. The next line has nn

integers s1,s2,…,sns1,s2,…,sn

, separated with single space, indicating serial number of each chip.

1≤T≤10001≤T≤1000

3≤n≤10003≤n≤1000

0≤si≤1090≤si≤109

There are at most 1010

testcases with n>100n>100

OutputFor 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

1.暴力竟然能过(mmp)暴力出奇迹。。
2.正解:01字典树,附上神仙的博客~
https://www.cnblogs.com/letlifestop/p/10262761.html
下面是01字典树

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int sto[N][4],tot,a[1005];
long long flag[N],com[N];
void init()
{
   
    for(int i=0;i<=tot;i++)
    {
   
        flag[i]=0;
        com[i]=0;
        for(int j=0;j<3;j++)
        {
   
            sto[i][j]=0;
        }
    }
    tot=0;
}
void add(long long a)
{
   
    int u=0;
    for(int i=32;i>=0;i--)
    {
   
        int tmp=(a>>i)&1;
        if(sto[u][tmp]==0)
            sto[u][tmp]=++tot;
        flag[sto[u][tmp]]++;
        u=sto[u][tmp];
    }
    com[u]=a;
}
void Erase(long long a)
{
   
    int u=0;
    for(int i=32;i>=0;i--)
    {
   
        int tmp=(a>>i)&1;
        flag[sto[u][tmp]]--;
        u=sto[u][tmp];
    }
}
long long query(long long a)
{
   
    int u=0;
    for(int i=32;i>=0;i--)
    {
   
        int tmp=(a>>i)&1;
        if(sto[u][tmp^1]>0)
        {
   
            if(flag[sto[u][tmp^1]]>0)
                u=sto[u][tmp^1];
            else
                u=sto[u][tmp];
        }
        else
        {
   
            if(flag[sto[u][tmp]]>0)
                u=sto[u][tmp];
            else
                u=sto[u][tmp];
        }
    }
    return com[u]^a;///返回异或值
}
int main()
{
   
    int t,n,m,i;
    tot=0;
    scanf("%d",&t);
    while(t--)
    {
   
        init();
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
   
            scanf("%lld",&a[i]);
            add(a[i]);
        }
        long long maxx=0;
        for(int i=1;i<=n;i++)
        {
   
            Erase(a[i]);
            for(int j=i+1;j<=n;j++)
            {
   
                Erase(a[j]);
                maxx=max(query(a[i]+a[j]),maxx);
                add(a[j]);
            }
            add(a[i]);
        }
        cout<<maxx<<'\n';
    }
    return 0;
}

全部评论

相关推荐

过往烟沉:我说什么来着,java就业面就是广!
点赞 评论 收藏
分享
评论
1
收藏
分享
正在热议
# 25届秋招总结 #
443331次浏览 4520人参与
# 春招别灰心,我们一人来一句鼓励 #
42187次浏览 537人参与
# 北方华创开奖 #
107468次浏览 600人参与
# 地方国企笔面经互助 #
7973次浏览 18人参与
# 同bg的你秋招战况如何? #
77166次浏览 569人参与
# 实习必须要去大厂吗? #
55811次浏览 961人参与
# 阿里云管培生offer #
120419次浏览 2220人参与
# 虾皮求职进展汇总 #
116310次浏览 887人参与
# 如果你有一天可以担任公司的CEO,你会做哪三件事? #
11683次浏览 289人参与
# 实习,投递多份简历没人回复怎么办 #
2454962次浏览 34861人参与
# 提前批简历挂麻了怎么办 #
149927次浏览 1978人参与
# 在找工作求抱抱 #
906096次浏览 9421人参与
# 如果公司给你放一天假,你会怎么度过? #
4762次浏览 55人参与
# 你投递的公司有几家约面了? #
33209次浏览 188人参与
# 投递实习岗位前的准备 #
1196037次浏览 18550人参与
# 机械人春招想让哪家公司来捞你? #
157648次浏览 2267人参与
# 双非本科求职如何逆袭 #
662384次浏览 7397人参与
# 发工资后,你做的第一件事是什么 #
12806次浏览 62人参与
# 工作中,努力重要还是选择重要? #
35906次浏览 384人参与
# 简历中的项目经历要怎么写? #
86937次浏览 1516人参与
# 参加完秋招的机械人,还参加春招吗? #
20153次浏览 240人参与
# 我的上岸简历长这样 #
452074次浏览 8089人参与
牛客网
牛客企业服务