Codeforces Round #618 (Div. 2)C. Anu Has a Function
Anu has created her own function ff : 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 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] 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 nn (1≤n≤105 ).
The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤109 ). Elements of the array are not guaranteed to be different.
Output nn integers, the reordering of the array with maximum value. If there are multiple answers, print any.
4
4 0 11 6
11 6 4 0
1
13
13
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.
这个题按照官方题解说的可以把这个函数写成这种形式: f(x,y)=(x|y)-y=x&(~y),(y的某一位为0的话x的这一位不变,y的某一位为1的话x的这一位为0)所以要求的式子可以写成这样:a1&(~a2)&(~a3).......&(~an),原问题转化为对数列进行排序使得a1&(~a2)&(~a3).......&(~an)最大。涉及到位运算所以把每个数都转化成二进制并提取每一位,贪心地从最高位开始分析(第32位)。对于第i位来说如果在这n个数里面只有一个数在这一位是1,而其他数在这一位是0,那么就把这一位加入队列并打上标记,输出的时候先输出这个数,再以任意顺序输出剩下的数。因为注意到这样一点,其实最终的排列结果只与一个数有关,就是贡献最大的那个数,把这个数排到第一位,剩下的数不论怎么排结果都一样。仅有一个数在第i位是1,其他数都是0的话选择把这一个数作为a1,其他数按位取反后第i位都变成1了,经过n-1次与操作这一位还是1,能保证所求的值尽可能大。如何确保答案最大呢?自然就是从高位到低位遍历,当第一次遇到某一位满足这样的条件,贡献最大的数也就确定了。
或者可以这么想,位运算的特点是二进制表示下不进位,所以各个位之间是独立无关的。因为要求的是最大值,所以依次从高到低贪心地分析...
#include <bits/stdc++.h>
using namespace std;
int a[][];
int vis[]={};
int main()
{
int n;
cin>>n;
int i,j;
queue<int>q;
for(i=;i<=n;i++)
{
int temp;
scanf("%d",&temp);
a[i][]=temp;
int j;
for(j=;j<=;j++)
{
a[i][j]=temp&;
temp>>=;
}
}
for(j=;j>=;j--)
{
int cnt=;
int mark=-;
for(i=;i<=n;i++)
{
if(a[i][j])
{
cnt++;
mark=i;
}
}
if(cnt==)
{
q.push(a[mark][]);
vis[mark]=;
break;
}
}
while(!q.empty())
{
int temp=q.front();
q.pop();
cout<<temp<<' ';
}
for(i=;i<=n;i++)
{
if(!vis[i])cout<<a[i][]<<' ';
}
return ;
}
Codeforces Round #618 (Div. 2)C. Anu Has a Function的更多相关文章
- Codeforces Round #618 (Div. 2)
题库链接 https://codeforces.ml/contest/1300 A. Non-zero 一个数组,每次操作可以给某个数加1,让这个数组的积和和不为0的最小操作数 显然如果有0的话,必须 ...
- Codeforces Round #618 (Div. 2) 小号上紫之路
这一场涨了不少,题也比较偏思维,正好适合我 A. Non-zero 我们记录这些数字的总和sum,并且记录0的个数zero,显然答案应该是这些0的个数,注意如果sum+zero==0的话答案要额外加一 ...
- [CF百场计划]#2 Codeforces Round #618 (Div. 2)
A. Non-zero Description: Guy-Manuel and Thomas have an array \(a\) of \(n\) integers [\(a_1, a_2, \d ...
- Codeforces Round #618 (Div. 1)C(贪心)
把所有数看作N块,后面的块比前面的块小的话就合并,这个过程可能会有很多次,因为后面合并后会把前面的块均摊地更小,可能会影响更前面地块,像是多米诺骨牌效应,从后向前推 #define HAVE_STRU ...
- Codeforces Round #618 (Div. 1)B(几何,观察规律)
观察猜测这个图形是中心对称图形是则YES,否则NO #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace ...
- Codeforces Round #618 (Div. 1)A(观察规律)
实际上函数值为x&(-y) 答案仅和第一个数字放谁有关 #define HAVE_STRUCT_TIMESPEC #include <bits/stdc++.h> using na ...
- Codeforces Round #618 (Div. 2)A. Non-zero
Guy-Manuel and Thomas have an array aa of nn integers [a1,a2,…,an ]. In one step they can add 11 to ...
- 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 ...
- 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 ...
随机推荐
- linux 安装tar 命令
yum install -y tar 查看版本 tar --version
- SpringBoot图文教程4—SpringBoot 实现文件上传下载
有天上飞的概念,就要有落地的实现 概念+代码实现是本文的特点,教程将涵盖完整的图文教程,代码案例 文章结尾配套自测面试题,学完技术自我测试更扎实 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例 ...
- luckyframe的一些坑
建议使用idea运行 1.第一次运行访问http://localhost:8088/LuckyFrameServer 2.luckyframe提示“javax.net.ssl.SSLKeyExcept ...
- Windows Server 2012 忘记登录密码怎么办?
Windows Server 2012系统 忘记登录密码处理方法,此方法在其他 Server 系统应该是通用的(其他系统未做测试,请知悉) 电脑 Windows Server 2012系统 做好的U盘 ...
- apt-get install 下载速度慢问题的解决
参考博客:https://blog.csdn.net/weixin_38538240/article/details/99665433 重点:在software&updates中更换为国内的源 ...
- linux建立动态库的软链接
复制动态库: /home/wmz/anaconda3/lib/ 删除原链接: 建立新链接: /home/wmz/anaconda3/lib/libstdc++.so. 问题的起源是,安装anacond ...
- 箭头函数 this指向问题
1.为什么要用箭头函数 简洁 易用 固定this 指向(箭头函数在this定义时候生效) 2.箭头函数分析this指向 1.this指向调用函数的对象 情况1 var obj={ a:"1& ...
- 小白科普:Netty有什么用?
随着移动互联网的爆发性增长,小明公司的电子商务系统访问量越来越大,由于现有系统是个单体的巨型应用,已经无法满足海量的并发请求,拆分势在必行. 在微服务的大潮之中, 架构师小明把系统拆分成了多个服务,根 ...
- 设备驱动基础学习--platform driver简单实现
platform是一条虚拟的总线.设备用platform_device表示,驱动用platform_driver进行注册,Linux platform driver机制和传统的device drive ...
- UseIIS
asp.net core webapi的program.cs 文件中,要加上 使用IIS进程内,可以大幅提高处理速度