题解 CF1299A 【Anu Has a Function】
ShineEternal
2020-02-10 10:44:25
[更佳的阅读效果](https://blog.csdn.net/kkkksc03/article/details/104245002)
## description:
- 定义一个函数 $\operatorname{f}(x,y)=\operatorname{f}(x|y)-y$。
- 给定一个长度为 $n$ 数列 $a$,定义
$$\operatorname{f}(\operatorname{f}..\operatorname{f}(\operatorname{f}(a_1,a_2),a_3),...a_{n-1}),a_n)$$
为这个数列的值。
- 现在,请你将数列改变一种顺序,使得最后的值最大。
- 输出你改变后的数列。
- $n\le 10^5$。
- translate by @[ShineEternal](https://www.luogu.com.cn/user/45475)。
## solution:
这题我们发现只有某一位只有一个 $1$ 时才对答案有贡献。
所以枚举每一位每个数,每次把符合条件的数字提到前面即可(其他随便排)。
时间复杂度:$O(32\times n)$ ?
## code:
```cpp
#include<cstdio>
#include<algorithm>
using namespace std;
int a[200005],b[200005];
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=32;i>=1;i--)
{
int tag=0,tmp;
for(int j=1;j<=n;j++)
{
if((a[j]>>(i-1))&1==1&&b[j]==0)
{
tmp=j;
if(tag==1)
{
tag=0;
break;
}
tag=1;
}
}
if(tag==1)
{
printf("%d ",a[tmp]);
b[tmp]=1;
}
}
for(int i=1;i<=n;i++)
{
if(b[i]==0)
{
printf("%d ",a[i]);
}
}
printf("\n");
return 0;
}
```