Perfect Security
Perfect Security
https://ac.nowcoder.com/acm/problem/112567
分析
由于我们需要得到字典序最小的生成序列
所以我们考虑贪心
对可以随机选取的数组进行构造01Trie
树
进而我们遍历数组A[]
每次贪心往小的点走
找到之后我们删除它
那么我们再写一个Delete()
函数即可
时间复杂度:
代码
//CF923C #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #define LL long long #define Cl(X,Y) memset((X),(Y),sizeof(X)) #define FOR(i,A,B) for(int i=A;i<=B;i++) #define BOR(i,A,B) for(int i=A;i>=B;i--) #define Debug(X) cerr<<#X<<" = "<<X<<" " #define Lowbit(X) (X & (-X)) #define Skip cout<<endl; #define INF 0x3f3f3f3f #define Mod 998244353 #define Rson (X<<1|1) #define Lson (X<<1) using namespace std; using namespace std; const int N=3e5+3; const int md=1e9+7; const int inf=1e9+3; int n,nm,res; int a[N]; int b[N]; int to[N*30][2]; int sm[N*30]; inline void Update(int x) { int v=1; BOR(i,29,0) { int z=0; if(x&(1<<i)) z=1; sm[v]++; if(to[v][z]==0)to[v][z]=++nm; v=to[v][z]; } sm[v]++; } inline void Delete(int x) { int v=1; res=0; BOR(i,29,0) { int z=0; if(x&(1<<i)) z=1; sm[v]--; if(sm[to[v][z]]==0)res|=(1<<i),z^=1; v=to[v][z]; } sm[v]--; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); nm=1; cin>>n; FOR(i,1,n) cin>>a[i]; FOR(i,1,n) { cin>>b[i]; Update(b[i]); } FOR(i,1,n) { Delete(a[i]); cout<<res<<" "; } cout<<endl; system("pause"); return 0; }