zoukankan      html  css  js  c++  java
  • HDU 5536 字典树

    题意:就是公式。

    这现场赛O(n^3)能过,觉得太没天理了。

    做法:字典树,枚举两个数,然后在字典树上贪心的跑。

    #include <bits/stdc++.h>
    
    using namespace std;
    
    const int MAXN = 100010;
    
    struct Trie {
        int ch[2],size;
    }T[MAXN];
    
    
    int root = 1,tot = 1;
    
    void Insert(int x) {
        int o = root;
        T[o].size++;
    
        for(int k = 30; k >=0; k--) {
            int c;
            if(x&(1<<k)) c = 1;
            else c = 0;
            if(!T[o].ch[c]) T[o].ch[c] = ++tot;
            o = T[o].ch[c];
            T[o].size++;
        }
    }
    
    void Delete(int x) {
        int o = root;
        T[o].size--;
    
        for(int k = 30; k >=0; k--) {
            int c;
            if(x&(1<<k)) c = 1;
            else c = 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;
            if(x&(1<<k)) c = 1;
            else c = 0;
            if(c==1) {
                if(T[o].ch[0]&&T[T[o].ch[0]].size) o = T[o].ch[0];
                else o = T[o].ch[1],x^=(1<<k);
            }
            else {
                if(T[o].ch[1]&&T[T[o].ch[1]].size) o = T[o].ch[1],x^=(1<<k);
                else o = T[o].ch[0];
            }
        }
        return x;
    }
    
    int a[MAXN];
    
    int main()
    {
        //freopen("in.txt","r",stdin);
        int T_T,n;
        scanf("%d",&T_T);
        while(T_T--) {
            scanf("%d",&n);
            int ans = 0;
            for(int i = 1; i <= n; i++) scanf("%d",&a[i]);
            for(int i = 1; i <= n; i++)
                Insert(a[i]);
    
            for(int i = 1; i <= n; i++) {
                Delete(a[i]);
                for(int j = i+1; j <= n; j++) {
                    Delete(a[j]);
                    ans = max(ans,Query(a[i]+a[j]));
                    Insert(a[j]);
                }
                Insert(a[i]);
            }
            printf("%d
    ",ans);
            for(int i = 1; i<=tot; i++) T[i].ch[0] = T[i].ch[1] = T[i].size = 0;
            tot = 1;
        }
        return 0;
    }
  • 相关阅读:
    fastjson 使用方法
    算法
    SHA算法
    MD5算法
    kindle推送服务
    DLL劫持
    Hook编程
    Hook技术
    权限验证
    虚拟机
  • 原文地址:https://www.cnblogs.com/TreeDream/p/7868077.html
Copyright © 2011-2022 走看看