Codeforces Round #618 (Div. 2)C. Anu Has a Function

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=156=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),an1),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 (1n105 ).

The second line contains nn integers a1,a2,,ana1,a2,…,an (0ai109 ). 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 
using namespace std;
int a[100005][33];
int vis[100005]={0};
int main()
{
    int n;
    cin>>n;
    int i,j;
    queue<int>q;
    for(i=1;i<=n;i++)
    {
        int temp;
        scanf("%d",&temp);
        a[i][0]=temp;
        int j;
        for(j=1;j<=32;j++)
        {
            a[i][j]=temp&1;
            temp>>=1;
        }
    }
    for(j=32;j>=1;j--)
    {
        int cnt=0;
        int mark=-1;
        for(i=1;i<=n;i++)
        {
            if(a[i][j])
            {
                cnt++;
                mark=i;
            }
        }
        if(cnt==1)
        {
            q.push(a[mark][0]);
            vis[mark]=1;
            break;
        }    
    }
    while(!q.empty())
    {
        int temp=q.front();
        q.pop();
        cout<' ';
    }
    for(i=1;i<=n;i++)
    {
        if(!vis[i])cout<0]<<' ';
    }
    return 0;
}

 

 

你可能感兴趣的:(Codeforces Round #618 (Div. 2)C. Anu Has a Function)