【HDU - 5536】Chip Factory 【01 Trie 插入删除询问操作】

本文介绍了一种用于计算CPU芯片包装校验数的方法,该方法通过枚举芯片序列号并利用位运算实现最大校验数值的计算,适用于批量芯片管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)⊕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?
Input
The 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
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
分析 : 因为只有最多十组数据n是大于100的,所有我们枚举所有si+sj 的情况是允许的,关键是对于i j k 这三个序号不重复这一点怎么处理,虚删除操作,我们保留被删除的节点,但是标记其是没法走的路,删除i j 之后再重新插入,这时因为还保留当时的节点,就不用重新开空间了。
代码

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>pii;
#define first fi
#define second se
#define  LL long long

const int MAXN =1000+11;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;

int ch[MAXN*32][2],sz;
int num[MAXN*32];  // 记录每个节点的数目。 
void init(){
    memset(ch[0],0,sizeof(ch[0]));
    sz=1;
    memset(num,0,sizeof(num));
}
void Insert(LL a){
    int u=0;
    for(int i=31;i>=0;i--){
        int c=1&(a>>i);
        if(!ch[u][c]){
            memset(ch[sz],0,sizeof(ch[sz]));
            ch[u][c]=sz++;
        }
        u=ch[u][c];
        num[u]++;
    }
}
void Delete(LL a){
    int u=0;
    for(int i=31;i>=0;i--){
        int c=1&(a>>i);
        num[ch[u][c]]--;
        u=ch[u][c];
    }
}

LL Query(LL a){
    int u=0; LL ans=0;
    for(int i=31;i>=0;i--){
        int c=1&(a>>i);
        if(ch[u][c^1]&&num[ch[u][c^1]]) { //不仅要有这个节点,同时还要能够走这个节点 
            ans|=1<<i;       // 其实写成这样 ans|=(c^(c^1))<<i ; 对于入门的人可能更容易理解  
        //  puts("====");
            u=ch[u][c^1];
        }
        else 
            u=ch[u][c];
    }
    return ans;
}
LL a[MAXN];
int main(){
    int T;scanf("%d",&T);
    while(T--){
        init();
        int n;scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%lld",&a[i]);
            Insert(a[i]);
        }
        LL MAX =-1;
         for(int i=1;i<=n;i++){
            for(int j=i+1;j<=n;j++){
                Delete(a[i]); Delete(a[j]);
                //printf("a[i]  =%lld  a[j]==%lld  %lld\n",a[i],a[j],Query(a[i]+a[j]));
                MAX=max(Query(a[i]+a[j]),MAX);
                Insert(a[i]); Insert(a[j]);
             }
         }
         printf("%lld\n",MAX);
     }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值