Nikitosh和异或(01trie+最大异或对)

Nikitosh和异或

因为下面这段代码卡了接近一小时!

int s=p&1<<i; // wrong
int s=p>>i&1; // correct

题意:最大化两个异或对之和(还是看下面的题面吧)

思路

  1. 预处理前缀最大异或对,然后从后往前求后缀最大异或对即可
  2. 当然整个过程都利用了 01 t r i e 01trie 01trie性质,即在已经构建的 01 t r i e 01trie 01trie上贪心地选择路径

题面描述

#include "bits/stdc++.h"
#define hhh printf("hhh\n")
#define see(x) (cerr<<(#x)<<'='<<(x)<<endl)
using namespace std;
typedef long long ll;
typedef pair<int,int> pr;
inline int read() {int x=0;char c=getchar();while(c<'0'||c>'9')c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return x;}

const int maxn = 4e5+10;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;
const double eps = 1e-7;

int N;
int a[maxn];
int dp[maxn];
int node[maxn<<5][2], tot;

inline void insert(int p) {
    int now=0;
    for(int i=30; i>=0; --i) {
        int s=p>>i&1;
        if(!node[now][s]) node[now][s]=++tot;
        now=node[now][s];
    }
}

inline int match(int p) {
    int now=0, ans=0;
    for(int i=30; i>=0; --i) {
        int s=p>>i&1;
        if(node[now][!s]) ans|=1<<i, now=node[now][!s];
        else now=node[now][s];
    }
    return ans;
}

int main() {
    //ios::sync_with_stdio(false); cin.tie(0);
    N=read();
    for(int i=1; i<=N; ++i) a[i]=a[i-1]^read();
    insert(0);
    for(int i=1; i<N; ++i) dp[i]=max(dp[i-1],match(a[i])), insert(a[i]);
    for(int i=0; i<=tot; ++i) node[i][0]=node[i][1]=0;
    tot=0, insert(a[N]); int pre=0; ll ans=0;
    for(int i=N-1; i; --i)
        ans=max(ans,ll(dp[i])+(pre=max(pre,match(a[i])))), insert(a[i]);
    cout<<ans<<endl;
}
全部评论

相关推荐

我看标题以为40W,我觉得最差也得40k,点进去一看40块。你永远想不到客户的预算有多低...&nbsp;要求&ldquo;前端使用vue+element开发一个pc端宠物网站和vue+vant开发一个移动端网站,类型是宠物网站的。客户预算40&rdquo;&nbsp;全网最受欢迎的嵌入式面经面经一共32篇文章,12w+字数,包含全部最新的面试必问考点,4.7w+同学学习,2800+订阅,非常适合在找工作面经薄弱的同学,3000+订阅还会涨价,提前订阅提前享受,持续更新中。原帖链接:https://www.nowcoder.com/creation/manager/columnDetail/MJNwoMc
野猪不是猪🐗:哎呀,看来这位客户预算确实挺让人意外的呢!不过,别灰心,有时候客户的预算有限,但项目完成后说不定能带来意想不到的收获呢!😊 至于你提到的嵌入式面经,听起来好像很棒的样子!如果你对求职有帮助,那确实值得订阅学习哦!悄悄告诉你,点击我的头像,我们可以私信聊聊更多求职经验和技巧哦~🎉 对了,你对Vue和Element/Vant的开发有什么疑问或者想要分享的经验吗?我们可以一起探讨一下~😉
点赞 评论 收藏
分享
点赞 评论 收藏
分享
真得找个班上:真是白嫖学生劳动力脸都不要了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务