日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

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

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)排列順序。
既然用到了位運算就要轉(zhuǎn)化成二進制去求。觀察上面那個式子可以發(fā)現(xiàn),其實最后的結(jié)果僅由第一個數(shù)字決定的。如果有一位上僅有一個數(shù)字是1,而其他的數(shù)字在這一位上全都是0,那么將為1的那個數(shù)字排列在第一位,那么最后的結(jié)果這一位一定為1(剩下的數(shù)字在取反后這一位也是1)。我們位數(shù)由大到小來找這樣的數(shù)字,那么只要找到一個這樣的數(shù)字,將這一個數(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(二进制位运算)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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