题目链接:C. Anu Has a Function

题目

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?

输入

The first line contains a single integer n (1≤n≤10^5).

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

输出

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

样例

input

4
4 0 11 6

output

11 6 4 0

input

1
13

output

13

提示

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.

题意

有函数 f(x, y) = x|y - y
把输入的 n 个数按某种顺序排列,层层进行 f 函数,求得出来的值最大的排序方式。

解题思路

两个数位或之后再减去第二个数,实际上就是在二进制的角度把第一个数中与第二个数重复的 1 给去掉。

例:11 | 6 - 6

操作编号 十进制 二进制
1 11 1011
2 6 110
3 11 | 6 = 15 1111
4 5 - 6 = 9 1001

15 - 6 = 9 的二进制可以发现,减法实际上就是从 15 里把与 6 对应的 1 给去掉。从结果来看 9 实际上就是从 11 中,把 6 中对应位置也有的 1 给删掉。所以要最大的结果,就是一直减去对应位置的 1 之后,剩下的数最大即可。因此只需要确定排在最前面的数,后面的数顺序都无所谓。

首先这个数需要有 单独存在1

其次如果有多个数都有单独存在的 1 ,需要找到 剩下的 1 位数最高 的数

顺便,这里借用了 bitset 容器,bitset 容器也是 STL 里的一个容器,由于太过不常用,所以我依然不考虑做专门介绍。这里简单说明:

  1. 容器声明的时候尖括号里写的是位容器的位数
  2. 给位容器赋值可以用等于号赋值 bitset<50> b = a[i];
  3. b[i] 可以访问第 i 位的值(0 或 1)【0 号位置是最低位

代码

#include <iostream>
#include <bitset>using namespace std;int main() {ios::sync_with_stdio(0); cin.tie(0);int w[32], s[32], a[100009];for (int i = 0; i < 32; i++) w[i] = s[i] = 0;int t; cin >> t;for (int i = 0; i < t; i++) {cin >> a[i]; bitset<32> b = a[i];for (int j = 0; j < 32; j++) if (b[j]) w[j] += 1, s[j] = a[i];}int he;for (int i = 31; i >= 0; i--) if (w[i] == 1) {he = s[i]; break;}for (int i = 0; i < t; i++) if (a[i] == he) {int t = a[0]; a[0] = a[i]; a[i] = t;break;}for (int i = 0; i < t; i++) {if (i) cout << ' ';cout << a[i];} cout << endl;return 0;
}

请多多支持猹的个人博客 H_On 个人小站 啊啊啊~拜托惹 > ~ <
因为猹的小站真的还挺可的,所以那边更新的也比较勤奋,感谢关注~我会努力的(ง •_•)ง

【Codeforces Round#618 (Div. 2)】C. Anu Has a Function 题解相关推荐

  1. Codeforces Round #618 (Div. 1)-----A. Anu Has a Function

    提示: Codeforces Round #618 (Div. 1)-----A. Anu Has a Function 题意: 定义一个函数f(x , y) = f(x | y) - y 给定一个长 ...

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

    Anu has created her own function f: f(x,y)=(x|y)−y where | denotes the bitwise OR operation. For exa ...

  3. 寒假刷题13: Anu Has a Function Codeforces Round #618 (Div. 2) C

    题目链接: Anu Has a Function 题目解析: 观察函数f(x,y)定义:(x|y)-y 即 取出来x里是1但是y里不是1的地方 也就是 x&(~y) (也可以列真值表) 因此题 ...

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

    C. Anu Has a Function time limit per test1 second memory limit per test256 megabytes inputstandard i ...

  5. Codeforces #618 div.2 C. Anu Has a Function

    C. Anu Has a Function time limit per test1 second memory limit per test256 megabytes inputstandard i ...

  6. 贪心 ---- Codeforces Round #618 (Div. 2)B. Assigning to Classes+贪心[证明过程]

    题目链接 题目大意:给你2∗n2*n2∗n个数,将这些数分成2个集合使得两个集合中位数的差值最小 解题思路:我懵了一个结论就是排序之后取中间的两个数然后就ac了 我们先对这些数字从小到大排序 证明:1 ...

  7. Codeforces Round #618 (Div. 2)-B. Assigning to Classes

    Reminder: the median of the array [a1,a2,-,a2k+1] of odd number of elements is defined as follows: l ...

  8. Codeforces Round #618 (Div. 2)-Non-zero

    Guy-Manuel and Thomas have an array a of n integers [a1,a2,-,an]. In one step they can add 1 to any ...

  9. Codeforces Round #700 (Div. 2)(B,C,D1,D2详细题解)

    C C C是一个非常有意思的题(赛后被hack哈哈哈哈) 其他似乎就没什么好玩的了 1480 B.The Great Hero(模拟) 由于需要打死每个怪物,打死第 i i i个怪物需要攻击 k i ...

最新文章

  1. 这种吉他演奏方式,太牛了啊
  2. .NET Core开发实战(定义API的最佳实践)Source Generators版
  3. RUNOOB python练习题33 使用join方法实现用逗号分隔列表
  4. Element NavMenu
  5. SC2012 Orchestrator - 文档及资源链接
  6. Android之帮助文档
  7. Android 通过WebService进行网络编程,使用工具类轻松实现
  8. 奇异的Pinvoke调用
  9. MIPI CSI转TTL/LVDS/BT656/BT601/BT1120
  10. hadoop put命令的格式_【Hadoop篇】--Hadoop常用命令总结
  11. 弹性均质圆环法计算过程_盾构隧道衬砌管片计算方法的比较
  12. 无法打开网上邻居计算机,win7网上邻居在哪 无法访问怎么办【图文】
  13. DM6437烧写总结
  14. 环境猫室内监测仪——您的空气智能管家
  15. DSP芯片TMS320F2812之ADC模块的说明及使用步骤
  16. UI设计实用经验分享
  17. 微信连wifi3.1总结
  18. 欢迎大家加入Xcode公社
  19. Uniapp进行App云打包—安卓Android端
  20. Spyder 中 Reloaded modules 错误的解决方法

热门文章

  1. Java面试题-进阶篇(2022.4最新汇总)
  2. IDEA 官方最认可的神级框架!SpringBoot 已成气候!
  3. linux 流量 脚本,实时查看linux网卡流量脚本
  4. (100天2小时第二十九天)几种常见的图像噪声及去噪处理算法
  5. BootstrapTable去掉表格与分页的空白
  6. HBuilderX 使用内置终端打开命令框 操作文件
  7. 面试常用mysql语句_50个常用的笔试、面试sql语句
  8. Mac电脑如何使用时间机器进行备份?
  9. excel 查询 表关联_在Excel中计算查询表
  10. MATLAB/Simulink中的S函数报错