zoukankan      html  css  js  c++  java
  • CodeForces 706D Vasiliy's Multiset (字典树查询+贪心)

    题意:最开始的时候有一个集合,集合里面只有一个元素0,现在有q次操作,操作分为3种:

    + x: 表示向集合中添加一个元素x

    - x:表示删除集合中值为x的一个元素

    ? x:表示查询集合中与x异或的最大值为多少

    析:这是一个字典树的应用,不过确实没看出来。。。。主要思想是这样,先用10进制数,转成二进制数,记下每个结点的0,1的个数,这样增加和删除,就是对01的删除,

    剩下的就是查询,那么尽量让0和1XOR是最大的,所以,对于给定数,我们要去尽量他的XOR数,如果找到就加上,找不到,就找下一个。这样就是最大的。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <stack>
    using namespace std;
    
    typedef long long LL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const LL LNF = 100000000000000000;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 4e6 + 5;
    const int mod = 1e9 + 7;
    const char *mark = "+-*";
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    int n, m;
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
    }
    inline LL Max(LL a, LL b){  return a < b ? b : a; }
    inline LL Min(LL a, LL b){  return a > b ? b : a; }
    inline int Max(int a, int b){  return a < b ? b : a; }
    inline int Min(int a, int b){  return a > b ? b : a; }
    int ch[maxn][2];
    int t[maxn];
    int cnt = 0;
    
    void add(int x){
        int k = 1;
        for(int i = 30; i >= 0; --i){
            int j = ((1<<i) & x) > 0;
            if(!ch[k][j])  ch[k][j] = ++cnt;
            k = ch[k][j];
            ++t[k];
        }
    }
    
    void del(int x){
        int k = 1;
        for(int i = 30; i >= 0; --i){
            int j = ((1<<i) & x) > 0;
            k = ch[k][j];
            --t[k];
        }
    }
    
    int query(int x){
        int k = 1;
        int ans = 0;
        for(int i = 30; i >= 0; --i){
            int j = ((1<<i) & x) == 0;
            if(t[ch[k][j]]){
                ans |= (1<<i);
                k = ch[k][j];
            }
            else k = ch[k][1-j];
        }
        return ans;
    }
    
    int main(){
        while(scanf("%d", &n) == 1){
            cnt = 1;
            memset(ch, 0, sizeof(ch));
            memset(t, 0, sizeof(t));
            add(0);
            while(n--){
                char s[3];
                int x;
                scanf("%s %d", s, &x);
                if(s[0] == '+')  add(x);
                else if(s[0] == '-')  del(x);
                else   printf("%d
    ", query(x));
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    LeetCode456. 132模式
    LeetCode455. 分发饼干
    LeetCode454. 四数相加 II
    LeetCode453. 最小移动次数使数组元素相等
    matchMedia 媒体查询结果
    异常捕获
    常用xpath选择器和css选择器总结
    python-爬虫中的extract()
    Python应用前景广阔,怎么学才能快速上手?
    Python 为什么要有 pass 语句?
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/5771127.html
Copyright © 2011-2022 走看看