• BZOJ4269: 再见Xor(线性基)


    Time Limit: 10 Sec  Memory Limit: 512 MB
    Submit: 474  Solved: 291
    [Submit][Status][Discuss]

    Description

    给定N个数,你可以在这些数中任意选一些数出来,每个数可以选任意多次,试求出你能选出的数的异或和的最大值和严格次大值。

    Input

    第一行一个正整数N。
    接下来一行N个非负整数。

    Output

    一行,包含两个数,最大值和次大值。

    Sample Input

    3
    3 5 6

    Sample Output

    6 5

    HINT

    100% : N <= 100000, 保证N个数不全是0,而且在int范围内

    Source

    一眼线性基,取最大值是最基本的操作,就是一路异或过去,能变大就异或

    次大值可以由最大值和最小值异或得到

    // luogu-judger-enable-o2
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int MAXN = 1e5 + 10,  B = 31;
    inline int read() {
        char c = getchar(); int x = 0, f = 1;
        while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
        while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
        return x * f;
    }
    int N, P[MAXN];
    void Insert(int x) {
        for(int i = B; i >= 0; i--) {
            if(x >> i) {
                if(P[i]) x = x ^ P[i];
                else {P[i] = x; return ;}
            }
        }
    }
    int QueryMax() {
        int ans = 0;
        for(int i = B; i >= 0; i--)
            if((ans ^ P[i]) > ans)
                ans = ans ^ P[i];
        return ans;
    }
    int QueryMin() {
        for(int i = 0; i <= B; i++)
            if(P[i]) 
                return P[i];
    }
    main() { 
    #ifdef WIN32
        freopen("a.in", "r", stdin);
    #endif
        N = read();
        for(int i = 1; i <= N; i++) {
            Insert(read());
        }
        int Mx = QueryMax(), Mn = QueryMin();
        printf("%d %d", Mx, Mx ^ Mn);
    }
  • 相关阅读:
    我读过的书 编程爱好者
    HarmonyOS ListContainer基础用法
    HarmonyOS ListContainer 读取网络json数组
    HarmonyOS Activity页面跳转
    HarmonyOS ListContainer 图文并排
    HarmonyOS 线性布局练习一 登录页面
    jsonserver 环境搭建及使用方法
    HarmonyOS 真机调试
    在win下设置C语言环境变量
    使用 Eclipse 调试 Java 程序的 10 个技巧
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/9192147.html
Copyright © 2020-2023  润新知