zoukankan      html  css  js  c++  java
  • Trie UVALive 7192 Chip Factory (15长春J)

    题目传送门

    题意:从n个数中选出不同的三个数a b c,使得(a+b)^c 最大

    分析:先将所有数字按位插入到字典树上,然后删除两个数字,贪心询问与剩下的数字最大异或值。 

    /************************************************
    * Author        :Running_Time
    * Created Time  :2015/11/1 14:58:49
    * File Name     :J.cpp
     ************************************************/
    
    #include <cstdio>
    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <cmath>
    #include <string>
    #include <vector>
    #include <queue>
    #include <deque>
    #include <stack>
    #include <list>
    #include <map>
    #include <set>
    #include <bitset>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    
    #define lson l, mid, rt << 1
    #define rson mid + 1, r, rt << 1 | 1
    typedef long long ll;
    const int N = 1e3 + 10;
    const int INF = 0x3f3f3f3f;
    const int MOD = 1e9 + 7;
    struct Trie {
        int ch[N*30][2], sz;
        int cnt[N*30];
        void init(void) {
            sz = 1; ch[0][0] = ch[0][1] = 0;
            memset (cnt, 0, sizeof (cnt));
        }
        void insert(int x)  {
            int u = 0;
            for (int c, i=30; i>=0; --i)   {
                c = x & (1 << i) ? 1 : 0;
                if (!ch[u][c])  {
                    ch[sz][0] = ch[sz][1] = 0;
                    ch[u][c] = sz++;
                }
                u = ch[u][c];
                cnt[u]++;
            }
        }
        void remove(int x)  {
            int u = 0;
            for (int c, i=30; i>=0; --i)    {
                c = x & (1 << i) ? 1 : 0;
                u = ch[u][c];
                cnt[u]--;
            }
        }
        int query(int x)    {
            int u = 0;
            for (int c, i=30; i>=0; --i)   {
                c = x & (1 << i) ? 1 : 0;
                if (c == 1) {
                    if (ch[u][0] && cnt[ch[u][0]])  u = ch[u][0];
                    else    u = ch[u][1], x ^= (1 << i);
                }
                else    {
                    if (ch[u][1] && cnt[ch[u][1]])  u = ch[u][1], x ^= (1 << i);
                    else    u = ch[u][0];
                }
            }
            return x;
        }
    }trie;
    int a[N];
    
    int main(void)    {
        int T;  scanf ("%d", &T);
        while (T--) {
            int n;  scanf ("%d", &n);
            for (int i=1; i<=n; ++i)    {
                scanf ("%d", &a[i]);
            }
            int ans = 0;
            trie.init ();
            for (int i=1; i<=n; ++i)    {
                trie.insert (a[i]);
            }
            for (int i=1; i<=n; ++i)    {
                trie.remove (a[i]);
                for (int j=i+1; j<=n; ++j)  {
                    trie.remove (a[j]);
                    ans = max (ans, trie.query (a[i] + a[j]));
                    trie.insert (a[j]);
                }
                trie.insert (a[i]);
            }
            printf ("%d
    ", ans);
        }
    
        return 0;
    }
    

      

    编译人生,运行世界!
  • 相关阅读:
    32位IP地址
    我们必须知道,我们终将知道。
    【Java 小白菜入门笔记 2.1】面向对象相关
    【Java 小白菜入门笔记 1.3】流程控制、数组和输入输出
    【Java 小白菜入门笔记 1.2】运算符、方法和语句
    【Java 小白菜入门笔记 1.1】常量和变量
    【论文笔记】PyTorch-BigGraph: A Large-scale Graph Embedding Framework(大规模图嵌入)
    【Java 小白菜入门笔记 1.0】简介与HelloWorld
    【NLP模型笔记】GloVe模型简介
    Python中的defaultdict函数
  • 原文地址:https://www.cnblogs.com/Running-Time/p/4956062.html
Copyright © 2011-2022 走看看