BZOJ 3689: 异或之

3 篇文章 0 订阅
2 篇文章 0 订阅

这题还不错? 我好菜啊
po姐说的很清楚啊QAQ 直接去看就好了
代码打着打着就发现打得好短(躺

不过似乎不算很快 没怎么看黄学长那种做法(好像很快

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+2,inf=(1ll<<31)-1;
char O[1<<14],*S=O,*T=O;
#define gc (S==T&&(T=(S=O)+fread(O,1,1<<14,stdin),S==T)?-1:*S++)
inline int read(){
    int x=0,f=1; char ch=gc;
    while(ch<'0' || ch>'9'){if(ch=='-')f=-1; if(ch<0)return 0; ch=gc;}
    while(ch>='0' && ch<='9'){x=(x<<1)+(x<<3)+ch-'0'; ch=gc;}
    return x*f;
}
int t[N*30][2],s[N*30],tot=0;
int a[N],p[N],n,b[31];
void ins(int x){
    int u=0;
    for(int i=30;~i;--i){
        int y=(x&b[i])>0;
        if(!t[u][y])t[u][y]=++tot;
        u=t[u][y],++s[u];
    }
}
int get(int x,int k){
    if(k>n)return inf;
    int u=0,w=0;
    for(int i=30;~i;--i){
        int y=(x&b[i])>0,o=s[ t[u][y] ];
        if(o>=k) u=t[u][y];
        else k-=o,u=t[u][y^1],w+=b[i];
    }
    return w;
}
struct P{int u,x;};
bool operator <(const P &x,const P &y){
    return x.u>y.u;
}
priority_queue<P>q;
int main(){
    n=read(); int m=read()<<1,i;
    b[0]=1; for(i=1;i<31;++i)b[i]=b[i-1]<<1;
    for(i=1;i<=n;++i) a[i]=read(),ins(a[i]),p[i]=2;
    for(i=1;i<=n;++i) q.push( (P){get(a[i],2),i} );
    while(m--){
        P o=q.top(); q.pop();
        if(m&1)printf("%d ",o.u);
        o.u=get(a[o.x],++p[o.x]);
        q.push(o);
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值