Anu has created her own function ff : f(x,y)=(x|y)−y where || denotes the bitwise OR operation. For example, f(11,6)=(11|6)−6=15−6=9. It can be proved that for any nonnegative numbers xx and yy value of f(x,y)f(x,y) is also nonnegative.

She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems.

A value of an array [a1,a2,…,an] is defined as f(f(…f(f(a1,a2),a3),…an−1),an) (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible?

Input

The first line contains a single integer nn (1≤n≤105 ).

The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤109 ). Elements of the array are not guaranteed to be different.

Output

Output nn integers, the reordering of the array with maximum value. If there are multiple answers, print any.

Examples
Input

 
4
4 0 11 6
Output

 
11 6 4 0
Input

Copy
1
13
Output

Copy
13 
Note

In the first testcase, value of the array [11,6,4,0][11,6,4,0] is f(f(f(11,6),4),0)=f(f(9,4),0)=f(9,0)=9f(f(f(11,6),4),0)=f(f(9,4),0)=f(9,0)=9 .

[11,4,0,6][11,4,0,6] is also a valid answer.

这个题按照官方题解说的可以把这个函数写成这种形式: f(x,y)=(x|y)-y=x&(~y),(y的某一位为0的话x的这一位不变,y的某一位为1的话x的这一位为0)所以要求的式子可以写成这样:a1&(~a2)&(~a3).......&(~an),原问题转化为对数列进行排序使得a1&(~a2)&(~a3).......&(~an)最大。涉及到位运算所以把每个数都转化成二进制并提取每一位,贪心地从最高位开始分析(第32位)。对于第i位来说如果在这n个数里面只有一个数在这一位是1,而其他数在这一位是0,那么就把这一位加入队列并打上标记,输出的时候先输出这个数,再以任意顺序输出剩下的数。因为注意到这样一点,其实最终的排列结果只与一个数有关,就是贡献最大的那个数,把这个数排到第一位,剩下的数不论怎么排结果都一样。仅有一个数在第i位是1,其他数都是0的话选择把这一个数作为a1,其他数按位取反后第i位都变成1了,经过n-1次与操作这一位还是1,能保证所求的值尽可能大。如何确保答案最大呢?自然就是从高位到低位遍历,当第一次遇到某一位满足这样的条件,贡献最大的数也就确定了。

或者可以这么想,位运算的特点是二进制表示下不进位,所以各个位之间是独立无关的。因为要求的是最大值,所以依次从高到低贪心地分析...

#include <bits/stdc++.h>
using namespace std;
int a[][];
int vis[]={};
int main()
{
int n;
cin>>n;
int i,j;
queue<int>q;
for(i=;i<=n;i++)
{
int temp;
scanf("%d",&temp);
a[i][]=temp;
int j;
for(j=;j<=;j++)
{
a[i][j]=temp&;
temp>>=;
}
}
for(j=;j>=;j--)
{
int cnt=;
int mark=-;
for(i=;i<=n;i++)
{
if(a[i][j])
{
cnt++;
mark=i;
}
}
if(cnt==)
{
q.push(a[mark][]);
vis[mark]=;
break;
}
}
while(!q.empty())
{
int temp=q.front();
q.pop();
cout<<temp<<' ';
}
for(i=;i<=n;i++)
{
if(!vis[i])cout<<a[i][]<<' ';
}
return ;
}

Codeforces Round #618 (Div. 2)C. Anu Has a Function的更多相关文章

  1. Codeforces Round #618 (Div. 2)

    题库链接 https://codeforces.ml/contest/1300 A. Non-zero 一个数组,每次操作可以给某个数加1,让这个数组的积和和不为0的最小操作数 显然如果有0的话,必须 ...

  2. Codeforces Round #618 (Div. 2) 小号上紫之路

    这一场涨了不少,题也比较偏思维,正好适合我 A. Non-zero 我们记录这些数字的总和sum,并且记录0的个数zero,显然答案应该是这些0的个数,注意如果sum+zero==0的话答案要额外加一 ...

  3. [CF百场计划]#2 Codeforces Round #618 (Div. 2)

    A. Non-zero Description: Guy-Manuel and Thomas have an array \(a\) of \(n\) integers [\(a_1, a_2, \d ...

  4. Codeforces Round #618 (Div. 1)C(贪心)

    把所有数看作N块,后面的块比前面的块小的话就合并,这个过程可能会有很多次,因为后面合并后会把前面的块均摊地更小,可能会影响更前面地块,像是多米诺骨牌效应,从后向前推 #define HAVE_STRU ...

  5. Codeforces Round #618 (Div. 1)B(几何,观察规律)

    观察猜测这个图形是中心对称图形是则YES,否则NO #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace ...

  6. Codeforces Round #618 (Div. 1)A(观察规律)

    实际上函数值为x&(-y) 答案仅和第一个数字放谁有关 #define HAVE_STRUCT_TIMESPEC #include <bits/stdc++.h> using na ...

  7. Codeforces Round #618 (Div. 2)A. Non-zero

    Guy-Manuel and Thomas have an array aa of nn integers [a1,a2,…,an ]. In one step they can add 11 to ...

  8. Codeforces Round #618 (Div. 2)-B. Assigning to Classes

    Reminder: the median of the array [a1,a2,-,a2k+1] of odd number of elements is defined as follows: l ...

  9. Codeforces Round #618 (Div. 2)-Non-zero

    Guy-Manuel and Thomas have an array a of n integers [a1,a2,-,an]. In one step they can add 1 to any ...

随机推荐

  1. Commercial Lighting: LED Ceiling Light, LED Ceiling Light

    Unlike ceiling lamps, floor lamps, chandeliers, lamps that can sometimes rely on "faces", ...

  2. 传奇服务端添加双倍经验卷的方法 双倍经验卷轴DB示例展示

    第一步我们在DBC数据库中添加好双倍经验卷轴DB,以下是现成的双倍经验卷DB,导入到DB里面就可以了. 222;双倍经验卷;31;0;1;20;0;0;265;0;0;0;0;0;0;0;0;0;0; ...

  3. 每日扫盲:eclipse快捷键 包括查找类、方法、变量汇总

    [Ct rl+T] 搜索当前接口的实现类 1. [ALT +/]    此快捷键为用户编辑的好帮手,能为用户提供内容的辅助,不要为记不全方法和属性名称犯愁,当记不全类.方法和属性的名字时,多体验一下[ ...

  4. spring注解注入:<context:component-scan>以及其中的context:include-filter>和 <context:exclude-filter>的是干什么的?

    转自:https://www.cnblogs.com/vanl/p/5733655.html spring注解注入:<context:component-scan>使用说明   sprin ...

  5. docker 环境部署

    docker 查看所有容器 docker ps  -a docker 查看所有running 容器: docker ps docker 停止全部容器: docker stop $(docker ps  ...

  6. Collection两个常见的集合类型: ArrayList可重复集有序 ,HashSet不可重复集

    package seday11; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; ...

  7. GitHub项目简介

    为了存放代码新建了一个GitHub账号,存放了一些比较常用的代码块,上面的模块大部分都能找到 index.html 文件直接在浏览器打开. 地址:https://github.com/liuzhou1 ...

  8. opencv:边缘提取

    #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...

  9. next.config.js

    const configs = { // 编译文件的输出目录 distDir: 'dest', // 是否给每个路由生成Etag generateEtags: true, // 页面内容缓存配置 on ...

  10. 联合查询:union

    1.联合查询:union 1.1 作用:将多条select语句的结果,合并到一起,称之为联合操作. 1.2 语法:( ) union ( ); 例子:(select name from info_or ...