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 ...
随机推荐
- 通过FormData对象可以组装一组用 [XMLHttpRequest]发送请求的键/值对,它可以更灵活方便的发送表单数据。
工作记录用 1 大概页面,点击选择按钮,选择文件,填写备注并可以上传前预览,然后点击上传按钮开始上传 2 html+js代码 <h2>Test</h2> <div id= ...
- 【Python】蟒蛇绘制(三种方式+import用法)
第一种方式不会出现函数重名问题,而第二种会.可以用第三种解决问题 方式一: #pythondraw.py import turtle #引用 绘制(海龟)库 turtle.setup(650,350, ...
- 扩展欧几里得求解同余方程(poj 1061)
设方程 ax + by = c , 若 gcd(a,b) 是 c的因子(记作gcd(a,b)|c)则方程有解,反之无解. 其中x0,y0是方程的一组特解 , d = gcd(a,b), poj1061 ...
- Vue - 过渡 列表过渡
列表的进入/离开过渡 获取不大于数组长度的随机数,作为插入新值的位置 <div id="app" class="demo"> <button ...
- android如何让checkbox实现互斥以及android验证端cession登录注意事项
1.CheckBox有一个监听器OnChangedListener,每次选择checkbox都会触发这个事件, 里边有一个参数isChecked,就是判断checkbox是否已经选上了的,可以在这判断 ...
- python+pygame的导弹追踪鼠标游戏设置和说明
1.效果图 2.注意事项,代码里有说明 3.完整的代码 #导出模块 import pygame,sys from math import * #设置RESIZABLE前,必须导出下面的模块,否则报错 ...
- python 第三方库安装
1.首先安装pip 2.在cmd中找到pip的安装路径,(一般在python的scripts文件中) 3.pip install 第三方库名称
- 逻辑运算符及其优先级,C语言逻辑运算符及其优先级详解
C 语言提供了以下三种逻辑运算符. 一元:!(逻辑非). 二元:&&(逻辑与).||(逻辑或). 以上三种逻辑运算符中,逻辑非 ! 的优先级最高,逻辑与 && 次之,逻 ...
- JavaScript对象,函数,作用域
JavaScript对象 在 JavaScript中,几乎所有的事物都是对象.JavaScript 对象是拥有属性和方法的数据. var car = {type:"Fiat", m ...
- 委托与事件--delegate&&event
委托 访问修饰符 delegate 返回值 委托名(参数); public delegate void NoReturnNoPara(); public void NoReturnNoParaMeth ...