hdu 5536 Chip Factory (01 Trie)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536

题面;

Chip Factory

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


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
 
实现代码;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int M = 1e3+10;
int tot;
int ch[32*M][2],vis[32*M];
ll val[32*M],a[M];

void init(){
    memset(vis,0,sizeof(vis));
    tot = 1;
    ch[0][0] = ch[0][1] = 0;
}

void ins(ll x){
    int u = 0;
    for(int i = 32;i >= 0;i --){
        int v = (x>>i)&1;
        if(!ch[u][v]){
            ch[tot][0] = ch[tot][1] = 0;
            val[tot] = 0;
            vis[tot] = 0;
            ch[u][v] = tot++;
        }
        u = ch[u][v];
        vis[u]++;
    }
    val[u] = x;
}

void update(ll x,int c){
    int u = 0;
    for(int i = 32;i >= 0;i --){
        int v = (x>>i)&1;
        u = ch[u][v];
        vis[u] += c;
    }
}

ll query(ll x){
    int u = 0;
    for(int i = 32;i >= 0;i --){
        int v = (x>>i)&1;
        if(ch[u][v^1]&&vis[ch[u][v^1]]) u = ch[u][v^1];
        else u = ch[u][v];
    }
    return x^val[u];
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int t,n,m;
    cin>>t;
    while(t--){
        cin>>n;
        ll mx = 0;
        init();
        for(int i = 1;i <= n;i ++)
            cin>>a[i],ins(a[i]);
        for(int i = 1;i <= n;i ++){
            for(int j = i+1;j <= n;j ++){
                update(a[i],-1); update(a[j],-1);
                mx = max(mx,query(a[i]+a[j]));
                update(a[i],1); update(a[j],1);
            }
        }
        cout<<mx<<endl;
    }
}
原文地址:https://www.cnblogs.com/kls123/p/10723409.html