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使用mount挂载samba共享文件夹
挂载smb的目录,使用读写644权限 mount -t cifs -o "rw,dir_mode=0644,file_mode=0644,username=username,password ...
- js实现页面的秒数倒计时
<button name="vcode_mail" class="btn btn-default" type="button" id= ...
- Docker镜像加速,设置国内源
源地址设置 在 /etc/docker/daemon.json 中写入如下内容(如果文件不存在请新建该文件) { "registry-mirrors": [ "https ...
- KM poj 2195
题意:给出一个地图,地图上有人和房子,问如何分配哪个人去哪个房子,走的路最短? 这道题是个完备匹配的情况下,问怎么才能走的路最少,可以用KM来做. 只不过KM算法是用来求解最大最优值,所以我们得改一下 ...
- go基础_切片
切片创建方式 1.通过数组创建 2.通过内置函数make创建 切片允许的操作 1.追加元素 2.通过内置函数make创建 package main import "fmt" fun ...
- C语言程序的错误和警告
一段C语言代码,在编译.链接和运行的各个阶段都可能会出现问题.编译器只能检查编译和链接阶段出现的问题,而可执行程序已经脱离了编译器,运行阶段出现问题编译器是无能为力的. 如果我们编写的代码正确,运行时 ...
- python中写入txt文件需要换行,以及\r 和\n
在Python中,用open()函数打开一个txt文件,写入一行数据之后需要一个换行 如果直接用 f.write(’\n’)只会在后面打印一个字符串’\n’,而不是换行’需要用 f.write(’\r ...
- report_delay_calculation/check_timing/report_annotated_parasitics/report_analysis_coverge
如何debug 一颗cell 或一段net 的delay, 常用的办法是用report_delay_calculation 报这颗cell 或这段net, 会得到形式如下的report, 从该rep ...
- 01-书城http状态405-此url不支持http方法get
错误: http状态405-此url不支持http方法get 原因:
- 华硕笔记本(i76700hq+nvidia goforce940mx)安装ubuntu18.04
Ubuntu的安装 今天终于下定决心要把笔记本安装成Ubuntu,但是网上的教材不够全面,我就想整合以下教程. 接下来详细讲解安装过程 1. Ubuntu iso镜像下载 下载地址:https://w ...