前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >BZOJ4269: 再见Xor(线性基)

BZOJ4269: 再见Xor(线性基)

作者头像
attack
发布2018-08-01 11:18:48
1770
发布2018-08-01 11:18:48
举报

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

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

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

代码语言:javascript
复制
// 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);
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-06-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Description
  • Input
  • Output
  • Sample Input
  • Sample Output
  • HINT
  • Source
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档