zoukankan      html  css  js  c++  java
  • 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 example, f(11,6)=(11|6)6=156=9. It can be proved that for any nonnegative numbers x and y value of 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?
    

    Input

    The first line contains a single integer n (1≤n≤105).
    
    The second line contains n integers a1,a2,,an (0≤ai≤109). Elements of the array are not guaranteed to be different.
    
    Output
    Output n integers, the reordering of the array with maximum value. If there are multiple answers, print any.
    

    Examples
    inputCopy

    4
    4 0 11 6
    

    outputCopy

    11 6 4 0
    

    inputCopy

    1
    13
    

    outputCopy

    13 
    

    Note

    In the first testcase, value of the array [11,6,4,0] is f(f(f(11,6),4),0)=f(f(9,4),0)=f(9,0)=9.
    [11,4,0,6] is also a valid answer.
    

    我们把a|b-b换成 a-a&b,也就是说使得a&b最小,那我们考虑什么时候最大,即所有数的最高位,只有一个为1不然一定在运算的过程中被削去,那么这个题就可以改成,在32位中找某一位只有一个数是1的那一个数,让他当第一。因为其余都会抵消。如果不存在,那么怎样输出都是0

    #include <bits/stdc++.h>
    using namespace std;
    const int N = 100003;
    int n, a[N];
    int main()
    {
    	cin >> n;
    	for (int i = 1; i <= n; ++i)
    	{
    		cin >> a[i];
    	}
    	for (int j = 30; ~j; --j)
    	{
    		int cnt = 0, p;
    		for (int i = 1; i <= n; ++i)
    			if ((a[i] >> j) & 1)
    				++cnt, p = i;
    		if (cnt == 1)
    		{
    			printf("%d ", a[p]);
    			for (int k = 1; k <= n; ++k)
    				if (k != p)
    				printf("%d ", a[k]);
    			return 0;
    		}
    	}
    	cout<<endl<<endl;
    	for (int i = 1; i <= n; ++i)
    		printf("%d ", a[i]);
    }
    
  • 相关阅读:
    webform文件上传加水印
    2017-6-6 ASP.NET Ajax版页面无刷新三级联动
    2017-6-5 Ajax应用
    转【 正则表达式】
    2017-6-4 JQuery中的选择器和动画 弹窗遮罩
    Linq 组合分页查询
    2017-6-2 Linq高级查询和函数
    2017-6-3 JQuery中的Dom操作和事件
    WebForm母版页
    WebForm内置对象:Application和ViewState、Repeater的Command用法
  • 原文地址:https://www.cnblogs.com/lunatic-talent/p/12798445.html
Copyright © 2011-2022 走看看