您的位置:首页 > 其它

BZOJ 3689: 异或之

2017-12-04 22:20 281 查看
这题还不错? 我好菜啊

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
,p
,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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: