前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Codeforces Round #618 (Div. 2)-C. Anu Has a Function

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

作者头像
风骨散人Chiam
发布2020-10-29 12:38:42
2070
发布2020-10-29 12:38:42
举报
文章被收录于专栏:CSDN旧文
代码语言: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]);
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/02/10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档