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

2020-10-29 12:38:42 浏览数 (1)

代码语言:javascript复制
Anu has created her own function f: 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 x and y value of 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

代码语言:javascript复制
The first line contains a single integer n (1≤n≤105).

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

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

Examples inputCopy

代码语言:javascript复制
4
4 0 11 6

outputCopy

代码语言:javascript复制
11 6 4 0

inputCopy

代码语言:javascript复制
1
13

outputCopy

代码语言:javascript复制
13 

Note

代码语言:javascript复制
In the first testcase, value of the array [11,6,4,0] is f(f(f(11,6),4),0)=f(f(9,4),0)=f(9,0)=9.
[11,4,0,6] is also a valid answer.

我们把a|b-b换成 a-a&b,也就是说使得a&b最小,那我们考虑什么时候最大,即所有数的最高位,只有一个为1不然一定在运算的过程中被削去,那么这个题就可以改成,在32位中找某一位只有一个数是1的那一个数,让他当第一。因为其余都会抵消。如果不存在,那么怎样输出都是0

代码语言:javascript复制
#include <bits/stdc  .h>
using namespace std;
const int N = 100003;
int n, a[N];
int main()
{
	cin >> n;
	for (int i = 1; i <= n;   i)
	{
		cin >> a[i];
	}
	for (int j = 30; ~j; --j)
	{
		int cnt = 0, p;
		for (int i = 1; i <= n;   i)
			if ((a[i] >> j) & 1)
				  cnt, p = i;
		if (cnt == 1)
		{
			printf("%d ", a[p]);
			for (int k = 1; k <= n;   k)
				if (k != p)
				printf("%d ", a[k]);
			return 0;
		}
	}
	cout<<endl<<endl;
	for (int i = 1; i <= n;   i)
		printf("%d ", a[i]);
}

0 人点赞