923c C. Perfect Security

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;
}

原文地址:https://www.cnblogs.com/invoid/p/8646166.html

时间: 2024-08-01 11:32:53

923c C. Perfect Security的相关文章

【CodeForces】947 C. Perfect Security 异或Trie

[题目]C. Perfect Security [题意]给定长度为n的非负整数数组A和数组B,要求将数组B重排列使得A[i]^B[i]的字典序最小.n<=3*10^5,time=3.5s. [算法]异或Trie [题解]对一个数组O(n log n)建立异或Trie,就能O(log n)判断任意一个数在这个数组中异或值最大的数. 所以对B建异或Trie(每个数字从高二进制位开始插入),然后数组A依次在Trie上跑,从上到下尽量跑向相同数字边,这样得到字典序最小,路径中顺便删除标记. 复杂度O(n

Codeforces 948D Perfect Security(字典树)

题目链接:Perfect Security 题意:给出N个数代表密码,再给出N个数代表key.现在要将key组排序,使key组和密码组的亦或所形成的组字典序最小. 题解:要使密码组里面每个数都找到能使其亦或和最小的数可以将key建成一棵字典树(这里建树方式很关键,可以每个数都从2^31开始建树,这样可以使我们在遍历树查询更加方便).之后再遍历密码组每次在字典树里面找到一个能使它亦或和最小的数,再将这个数从字典树中删掉...  字典树太久不写,很多东西都忘记了! 1 #include<bits/s

01Trie树 CF923C Perfect Security

CF923C Perfect Security 上下各n个数,求一种排列p,使上面的数i异或pi成为新的数i,求方案另字典序最小,输出该结果 01Trie树. 记录每个节点经过多少次. 每一次查询的时候将经过的点的标记加1. 那么当我们要去走某一个节点的时候,就去判断他的标记是否小于初始的值,如果不是那么就不能走. 教训告诉我们一定要想好细节再打代码,我没想好怎么处理初始标记就去写了个拓扑排序.低智低智... code: #include <iostream> #include <cst

Internet History, Technology, and Security(week8)——Security: Encrypting and Signing

Hiding Date from Ohters Security Introduction Alice and Bob是密码学.博弈论.物理学等领域中的通用角色之一.Alice(代表A)和Bob(代表B)一般是形容好人的,用来描述通讯双方.通讯中也会出现一些不怀好意的人(People with bad intent),比如窃听者(Eve),用E代表. Security is Always a Tradeoff,安全总是一种充满妥协的权衡.对于安全这回事,几乎不存在所谓的Perfect Secur

Codeforces Round #470 (Div 2) B 数学 C 二分+树状数组 D 字典树

Codeforces Round #470 B. Primal Sport 数学题,对 x2 和 x1 分解质因子即可. #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b;

&lt;Crazy Gobang - classic game&gt;Privacy policy

<Crazy gobang-classic game> respects and protects the personal privacy of all users using the service.In order to provide you with more accurate and personalized services, <Crazy gobang-classic game> will use and disclose your personal informa

[TicTacToe]-Privacy policy

"TicTacToe" respects and protects the personal privacy of all users using the service.In order to provide you with more accurate and personalized services, "TicTacToe" will use and disclose your personal information in accordance with

Bighead Fighter - Boarding the Peak of the Beast

Contact: Tom Contact number: +6045823657 Contact: [email protected] "Bighead Fighter - Boarding the Peak of the Beast" respects and protects the personal privacy of all users using the service.In order to provide you with more accurate and perso

iOS Privacy Policy

This application respects and protects the privacy of all users who use the service. In order to provide you with more accurate and personalized services, this application will use and disclose your personal information in accordance with the provisi