CodeForces Round # 618 div2 C题

Anu Has a Function

题干

Anu has created her own function f:f(x,y)=(x∣y)−yf : f(x,y)=(x|y)−yf:f(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=9f(11,6)=(11∣6)−6=15−6=9 . It can be proved that for any nonnegative numbers xxx and yyy value of f(x,y)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,…,ana_1,a_2,\dots,a_na1​,a2​,…,an​ is defined as f(f(…f(f(a1,a2),a3),…an−1),an)f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)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 n(1≤n≤105)n (1 \le n \le 10^5 )n(1≤n≤105).

The second line contains nnn integers a1,a2,…,an(0≤ai≤109)a_1, a_2, \ldots, a_n ( 0 \le a_i \le 10^9 )a1​,a2​,…,an​(0≤ai​≤109). Elements of the array are not guaranteed to be different.

Output

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

解题思路

直觉上,我们可以判断出在二进制层面f(x,y)f(x,y)f(x,y)运算的过程中不会出现某一个二进制位被借位的情况(因为一般情况下的减法有可能出现这种情况)

//这里是按照传统的二进制减法来理解,当然计算机实现减法是用补码啦

我们采用一种类似于数学归纳法的方式证明这个结论

将x,yx,yx,y的最低位记作x0,y0x_0,y_0x0​,y0​,若x0∣y0=0x_0|y_0=0x0​∣y0​=0则x0=y0=0,f(x0,y0)=0x_0=y_0=0,f(x_0,y_0)=0x0​=y0​=0,f(x0​,y0​)=0,若x0∣y0=1且x0=y0x_0|y_0=1\text{且}x_0=y_0x0​∣y0​=1且x0​=y0​,则减法也不会发生借位

若x0∣y0=1且x0=1,y0=0x_0|y_0=1且x_0=1,y_0=0x0​∣y0​=1且x0​=1,y0​=0显然不会借位,若x0∣y0=1且x0=0,y0=1x_0|y_0=1且x_0=0,y_0=1x0​∣y0​=1且x0​=0,y0​=1也是没有发生借位。

我们发现针对最低位不会发生借位,不难归纳出对于每一位也是这样的。那么这个函数可以写为按位运算的形式

f(x,y)=xyˉf(x,y)=x\bar{y}f(x,y)=xyˉ​其中yˉ\bar{y}yˉ​表示对y按位取反

那么f(f(…f(f(a1,a2),a3),…an−1),an)=a1a2ˉa3ˉ…anˉ=a1and(a2ora3or…an)f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)=a_1\bar{a_2}\bar{a_3}\dots \bar{a_n}=a_1~and~\text{~}(a_2~or~a_3~or\dots a_n)f(f(…f(f(a1​,a2​),a3​),…an−1​),an​)=a1​a2​ˉ​a3​ˉ​…an​ˉ​=a1​ and  (a2​ or a3​ or…an​)

由或运算的分配律知整个式子的结果就是由a1a_1a1​的选取决定的

那么我们怎么选取a1a_1a1​呢?当然是暴力出奇迹啦!,每一个aia_iai​都计算一下就好了,这里要注意使用一下前缀和、后缀和的思想保存[a1,ai][a_1,a_i][a1​,ai​]区间和[aj,an][a_j,a_n][aj​,an​]相或的结果。

#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=1e5+5;
int a[maxn],qzh[maxn],hzh[maxn];
int main(){int n;cin>>n;for(int i=1;i<=n;i++){cin>>a[i];qzh[i]=qzh[i-1]|a[i];}for(int i=n;i>=1;i--){hzh[i]=hzh[i+1]|a[i];}int ans=-1,t,maxi=0;for(int i=1;i<=n;i++){t=a[i]&~(qzh[i-1]|hzh[i+1]);if(t>ans){ans=t;maxi=i;}}cout<<a[maxi]<<" ";for(int i=1;i<=n;i++){if(i!=maxi)cout<<a[i]<<" ";}
}

Anu-Has-a-Function相关推荐

  1. 【Codeforces Round#618 (Div. 2)】C. Anu Has a Function 题解

    题目链接:C. Anu Has a Function 题目 Anu has created her own function f: f(x,y)=(x|y)−y where | denotes the ...

  2. 寒假刷题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) (也可以列真值表) 因此题 ...

  3. 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 ...

  4. 2月9号cf,c题 Anu Has a Function

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

  5. AC. Anu Has a Function

    A&C. Anu Has a Function Codeforces Round #618 (Div. 1&2) 题目链接 关键词 greedy math *1500 位运算 解题思路 ...

  6. Codeforces——C. Anu Has a Function

    Codeforces--C. Anu Has a Function Anu has created her own function f: f(x,y)=(x|y)−y where | denotes ...

  7. 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 ...

  8. 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 给定一个长 ...

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

    Anu has created her own function ff: f(x,y)=(x|y)−yf(x,y)=(x|y)−y where || denotes the bitwise OR op ...

  10. 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 ...

最新文章

  1. 4、通过uiautomatorviewer实现appium元素定位
  2. python3.7.3安装selenium2library_python+selenium自动化的准备 2:安装python 3.7.4 和selenium 2.53.1...
  3. 小议传统分层与新式分层,抑或与DDD分层
  4. kali破解WiFi时wlan0没有变wlan0mon_黑客入门干货:黑客使用 Aircrack-ng 破解 Wi-Fi 密码
  5. 100内奇数之和流程图_互联网人工智能编程语言Python之while循环详解
  6. 三维图像处理_【图像处理】用于三维物体检测的三维骨干网络
  7. 软件工程能调剂到计算机么,愿意调剂到计算机专业或软件工程专业
  8. 肿么查找联想笔记本的序列号
  9. 正菱台体积在线计算机,正四棱台体积,表面积,棱长,质量在线计算器_三贝计算网_23bei.com...
  10. 内外兼修:Oracle ACED熊军谈Oracle学习
  11. bootstrapTable 根据条件隐藏某列
  12. DataStream API
  13. MySQL之账号管理、建库以及四大引擎
  14. 使用 Vue 脚手架
  15. [转载]用Java开发企业级无线应用
  16. 双击CAD图标后进入南方Cass界面的解决方法
  17. 时间序列网络RNN,LSTM入门
  18. 浅谈STM32 USART串口中断配置函数USART_ITConfig()的编程思路
  19. MySQL压缩包安装方法
  20. word中的标题   设置了段前30磅 但是在每一页的最上边的标题显示的时候却显示不出来段前间距:

热门文章

  1. 关于使用selenium+python简单获取前程无忧数据
  2. jQuery lightBox plugin 灯箱效果
  3. 【日本动漫新番尝鲜】机动战士高达OO 第二季
  4. 炸弹人游戏开发系列(8):放炸弹
  5. 网络基础12--工程师向
  6. Spring框架--SpringMVC文件上床
  7. 在建工地扬尘在线监控系统推荐_扬尘在线监测系统在建筑工地的应用
  8. c语言微博创建转发和删除源代码,[转]QQ空间、新浪微博、腾讯微博等一键分享API链接代码...
  9. 远程教育在线考试系统
  10. Linux下对grub引导文件丢失进行恢复