hdu5536 Chip Factory(01字典樹)

題目描述

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:

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?
 

輸入

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.

  • 1≤T≤1000
  • 3≤n≤1000
  • 0≤s i≤109
  • There are at most 10 testcases with n > 100

 

輸出

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

題目大意:給你n個數,求

 

01字典樹模板題,就是多了個刪除操作。先把所有的數存進字典樹內,然後枚舉i,j,在字典樹刪除i,j,求亦或和最大值,再將i,j存入字典樹即可。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
int trie[31 * maxn][2];
int tot;
int z[maxn];
int siz[31 * maxn];
void insert(int j)
{
    int tmp = 0;
    for (int i = 31; i >= 0; --i)
    {
        int tmpp = (j >> i) & 1;
        if (!trie[tmp][tmpp])
            trie[tmp][tmpp] = ++tot;
        tmp = trie[tmp][tmpp];
        ++siz[tmp];
    }
}
void dele(int j)
{
    int tmp = 0;
    for (int i = 31; i >= 0; --i)
    {
        int tmpp = (j >> i) & 1;
        tmp = trie[tmp][tmpp];
        --siz[tmp];
    }
}
int findd(int j)
{
    int tmp = 0;
    int cnt = 0;
    for (int i = 31; i >= 0; --i)
    {
        int tmpp = (j >> i) & 1;
        if (trie[tmp][tmpp ^ 1] && siz[ trie[tmp][tmpp ^ 1] ])
        {
            cnt += 1 << i;
            tmp = trie[tmp][tmpp ^ 1];
        }
        else
            tmp = trie[tmp][tmpp];
    }
    return cnt;
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        memset(trie, 0, sizeof(trie));
        memset(siz, 0, sizeof(siz));
        int n;
        tot = 1;
        scanf("%d", &n);
        for (int i = 1; i <= n; ++i)
            scanf("%d", &z[i]);
        int ans = 0;
        for (int i = 1; i <= n; ++i)
            insert(z[i]);
        for (int i = 1; i < n; ++i)
        {
            dele(z[i]);
            for (int j = i + 1; j <= n; ++j)
            {
                dele(z[j]);
                ans = max(ans, findd(z[i] + z[j]));
                insert(z[j]);
            }
            insert(z[i]);
        }
        printf("%d\n", ans);
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章