Trie树。

要求字典序最小,所以由前到后贪心的选择。建一个trie树维护b数列。

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 300000*(20+5) ;
const int maxm = 30 + 10; int a[maxn],s[maxn],p0[maxn],p1[maxn],cnt;
int n,b; int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
} void build() {
n=read();
for(int i=1;i<=n;i++) a[i]=read();
for(int i=1,rhs,t;i<=n;i++) {
rhs=read();
t=0;
for(int j=29;j>=0;j--)
{
b=(rhs>>j&1);
if(b==0)
{
if(!p0[t]) p0[t]=++cnt;
t=p0[t];
}
else
{
if(!p1[t]) p1[t]=++cnt;
t=p1[t];
}
s[t]++;
}
} for(int i=1,k,t;i<=n;i++)
{
k=t=0;
for(int j=29;j>=0;j--)
{
b=(a[i]>>j)&1;
if(b==0)
if(p0[t]&&s[p0[t]])
{
t=p0[t];
k*=2;
//printf("0");
}
else
{
t=p1[t];
k=k*2+1;
//printf("1");
}
else
{
if(p1[t]&&s[p1[t]])
{
t=p1[t];
k=k*2+1;
}
else
{
t=p0[t];
k*=2;
}
}
s[t]--;
}
printf("%d ",a[i]^k);
}
printf("\n");
} void solve()
{ } int main()
{
build();
solve(); return 0;
}

923c C. Perfect Security的更多相关文章

  1. CodeForces 923C Perfect Security

    C. Perfect Security time limit per test3.5 seconds memory limit per test512 megabytes inputstandard ...

  2. Codeforces 948D Perfect Security(字典树)

    题目链接:Perfect Security 题意:给出N个数代表密码,再给出N个数代表key.现在要将key组排序,使key组和密码组的亦或所形成的组字典序最小. 题解:要使密码组里面每个数都找到能使 ...

  3. 【CodeForces】947 C. Perfect Security 异或Trie

    [题目]C. Perfect Security [题意]给定长度为n的非负整数数组A和数组B,要求将数组B重排列使得A[i]^B[i]的字典序最小.n<=3*10^5,time=3.5s. [算 ...

  4. 01Trie树 CF923C Perfect Security

    CF923C Perfect Security 上下各n个数,求一种排列p,使上面的数i异或pi成为新的数i,求方案另字典序最小,输出该结果 01Trie树. 记录每个节点经过多少次. 每一次查询的时 ...

  5. Codeforces 948D Perfect Security

    Perfect Security 题意:给你一个A[i]数组, 再给你一个B[i]数组, 现在用选取 B[i] 数组中的一个 去和 A[i] 数组里的一个元素去进行异或操作, B[i]数组的元素只能用 ...

  6. Codeforces 948D Perfect Security 【01字典树】

    <题目链接> 题目大意: 给定两个长度为n的序列,可以改变第二个序列中数的顺序,使得两个序列相同位置的数异或之后得到的新序列的字典序最小. 解题分析: 用01字典树来解决异或最值问题.因为 ...

  7. 2018.12.08 codeforces 948D. Perfect Security(01trie)

    传送门 01trie板子题. 给出两个数列,允许把第二个数列重新排列. 求使得两个数列每个位置对应的数的异或值和成为最小值的每个位置的异或和. 把第二个数列插入到01trie里面然后对于第一个数列中的 ...

  8. Codeforces 923 C. Perfect Security

    http://codeforces.com/contest/923/problem/C Trie树 #include<cstdio> #include<iostream> us ...

  9. [CodeForces948D]Perfect Security(01字典树)

    Description 题目链接 Solution 01字典树模板题,删除操作用个数组记录下就行了 Code #include <cstdio> #include <algorith ...

随机推荐

  1. iOS的内存分配

    iOS中的内存大致可以分为代码区,全局/静态区,常量区,堆区,栈区. 1.代码区 代码段是用来存放可执行文件的操作指令(存放函数的二进制代码),也就是说是它是可执行程序在内存中的镜像.代码段需要防止在 ...

  2. 金山WPS面试题

    1.windows的handle 1)是一个宏定义#define void* HANDLE 2) HANDLE提供了一种统一的方式去获得系统资源,并对其进行操作. 3) HANDLE使得程序设计的细节 ...

  3. uvaoj 10474 - Where is the Marble?(sort+lower_bound)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. 【转】一款已上市MMO手游地图同步方案总结

    转自游戏开发主席 1. 客户端地图格子的相关知识 在2.5D的MMO游戏里,角色是通过3D的方式渲染,2D的地图是通过2D的方式显示,所以在客户端一般会有三个坐标系: a) 3D坐标系:所有需要3D渲 ...

  5. javaee开发工具及环境配置过程

    在配置javaee开发环境的过程中遇到过很多问题,在此系统的整理一下我之前的配置过程 注:配置过程学习自<JSP&Servlet学习笔记(第二版)>详细过程可以阅读此书.在文章的最 ...

  6. 单元测试模块unittest使用学习

    工作原理: unittest中最核心的四个概念是:test case, test suite, test runner, test fixture. 一个TestCase的实例就是一个测试用例.什么是 ...

  7. 【python 3.6】从网站抓图并存放到本地路径

    #!/usr/bin/python # -*- coding: UTF-8 -*- _author_ = 'BH8ANK' import urllib.request import re import ...

  8. 【第五章】MySQL数据库的安全机制

    MySQL权限表MySQL用户管理MySQL权限管理SSL加密连接

  9. Attention注意力机制介绍

    什么是Attention机制 Attention机制通俗的讲就是把注意力集中放在重要的点上,而忽略其他不重要的因素.其中重要程度的判断取决于应用场景,拿个现实生活中的例子,比如1000个人眼中有100 ...

  10. 感知机(perceptron)