Codeforces 967E - Big Secret 【DFS序+线段树】



E. Big Secret

time limit per test   2 seconds

memory limit per test      256 megabytes

Vitya has learned that the answer forThe Ultimate Question of Life, the Universe, and Everything is not the integer 5442, but an increasing integer sequence a1,…,an In order to not reveal the secret earlier than needed, Vitya encryptedthe answer and obtained the sequence b1,…,bn using thefollowing rules:

·  b1=a1b1=a1

·  bi=aiai−1bi=aiai1 for all ifrom 2 to n where xy is the bitwise XORof  xand y.

It is easy to see that the originalsequence can be obtained using the rule ai=b1bi.However, some time later Vitya discovered that the integers bi in the cypher got shuffled, and it can happen that when decrypted usingthe rule mentioned above, it can produce a sequence that is not increasing. Inorder to save his reputation in the scientific community, Vasya decided to findsome permutation of integers bi so that the sequence ai=b1bi is strictly increasing. Help him find such a permutation or determine thatit is impossible.

Input

The first line contains a single integern (1≤n≤1051≤n≤105).

The second line contains n integers b1,…,bn1≤bi<260).

Output

If there are novalid permutations, print a single line containing "No".

Otherwise in the first line print theword "Yes", and in the second line print integers b′1,…,b′n — a valid permutation of integers bi. The unordered multisets {b1,…,bn}and {b′1,…,b′n}should be equal, i. e. for each integer xthe number of occurrences of xx in the first multiset should be equalto the number of occurrences of x in the second multiset. Apart fromthis, the sequence ai=b′1b′I should be strictly increasing.

If there are multiple answers, print anyof them.

Examples

Input

3
1 2 3

Output

No

Input

6
4 7 7 12 31 61

Output

Yes
4 12 7 31 7 61

Note

In the first example no permutation is valid.

In the second example the given answer lead to the sequence a1=4, a2=8 a3=15 a4=16, a5=23 a6=42

 


【题目链接】 link

【题意】

现在给你n个数,问是否存在一个这n个数的排列使得这些数的前缀异或值严格递增。

【思路】

显然对于前缀异或值我们需要从低位往高位符合条件的位置依次放1

于是我们先预处理出每个数最高位所在位置

然后就是每次对于当前前缀和从低位往高位枚举如果有0的地方看能不能用以该位为最高位的数存在,以此类推模拟即可。

#include <cstdio>
#include <bits/stdc++.h>
#include <cmath>
#include <map>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)

typedef long long ll;
const int maxn = 100005;
const ll mod = 1e9+7;
const int INF = 1e9;
const double eps = 1e-6;

int n;
ll num[maxn];
vector<ll>vec;
queue<ll>q[70];

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&num[i]);
        int now=-1;
        for(int j=60;j>=0;j--)
        {
            if((num[i]&(1LL<<j)))
            {
                now=j;
                break;
            }
        }
        q[now].push(i);
    }
    ll ans=0;
    for(int i=1;i<=n;i++)
    {
        int now=0;
        for(int j=0;j<=60;j++)
        {
            if((ans&(1LL<<j))==0&&q[j].size())
            {
                now=q[j].front();
                q[j].pop();
                break;
            }
        }
        if(now==0) return puts("No"),0;
        vec.push_back(num[now]);
        ans^=num[now];
    }
    puts("Yes");
    for(int i=0;i<vec.size();i++) printf("%lld ",vec[i]);
    puts("");
}





评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值