日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

Anu Has a Function CodeForces - 1300C(二进制位运算)

發(fā)布時(shí)間:2023/12/15 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Anu Has a Function CodeForces - 1300C(二进制位运算) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Anu has created her own function ff: f(x,y)=(x|y)?yf(x,y)=(x|y)?y where || denotes the bitwise OR operation. For example, f(11,6)=(11|6)?6=15?6=9f(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][a1,a2,…,an] is defined as f(f(…f(f(a1,a2),a3),…an?1),an)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≤1051≤n≤105).

The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1090≤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
1
13
Output
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.
經(jīng)過打表后可以發(fā)現(xiàn),f(x,y)=(x|y)-y=x&(~y),可以寫成這種形式。
那么我們就去求x1&(~x2)&( ~x3)&…&( ~xn)最大的最優(yōu)排列順序。
既然用到了位運(yùn)算就要轉(zhuǎn)化成二進(jìn)制去求。觀察上面那個(gè)式子可以發(fā)現(xiàn),其實(shí)最后的結(jié)果僅由第一個(gè)數(shù)字決定的。如果有一位上僅有一個(gè)數(shù)字是1,而其他的數(shù)字在這一位上全都是0,那么將為1的那個(gè)數(shù)字排列在第一位,那么最后的結(jié)果這一位一定為1(剩下的數(shù)字在取反后這一位也是1)。我們位數(shù)由大到小來找這樣的數(shù)字,那么只要找到一個(gè)這樣的數(shù)字,將這一個(gè)數(shù)字安排在第一位,剩下的隨便放就可以了。
代碼如下:

#include<bits/stdc++.h> #define ll long long using namespace std;const int maxx=1e5+100; int a[maxx]; int n;inline int fcs() {for(int i=30;i>=0;i--){int cnt=0;int pos;for(int j=1;j<=n;j++){if((a[j]&(1<<i))) cnt++,pos=j;}if(cnt==1) return pos;//找到這樣的一種數(shù)字}return 0; } int main() {scanf("%d",&n);for(int i=1;i<=n;i++) scanf("%d",&a[i]);int pos=fcs();if(pos==0) for(int i=1;i<=n;i++) cout<<a[i]<<" ";else {cout<<a[pos]<<" ";for(int i=1;i<=n;i++){if(i!=pos) cout<<a[i]<<" ";}}cout<<endl;return 0; }

努力加油a啊,(o)/~

總結(jié)

以上是生活随笔為你收集整理的Anu Has a Function CodeForces - 1300C(二进制位运算)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。