zoukankan      html  css  js  c++  java
  • A. Anu Has a Function

    Anu has created her own function ff: f(x,y)=(x|y)yf(x,y)=(x|y)−y where || denotes the bitwise OR operation. For example, f(11,6)=(11|6)6=156=9f(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][a1,a2,…,an] is defined as f(f(f(f(a1,a2),a3),an1),an)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 nn (1n1051≤n≤105).

    The second line contains nn integers a1,a2,,ana1,a2,…,an (0ai1090≤ai≤109). Elements of the array are not guaranteed to be different.

    Output

    Output nn integers, the reordering of the array with maximum value. If there are multiple answers, print any.

    Examples
    input
    Copy
    4
    4 0 11 6
    
    output
    Copy
    11 6 4 0
    input
    Copy
    1
    13
    
    output
    Copy
    13 
    Note

    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.

    a&(b)

    [a1,a2,,anwould be a1&(a2)&(an)

    做一个前缀后缀再遍历比较

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    #define ll              long long
    #define PII             pair<int, int>
    #define rep(i,a,b)      for(int  i=a;i<=b;i++)
    #define dec(i,a,b)      for(int  i=a;i>=b;i--)
    #define pb              push_back
    #define mk              make_pair
    using namespace std;
    int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979323846;
    const int mod = 998244353;
    const int N = 2e5 + 5;
    //if(x<0 || x>=r || y<0 || y>=c)
    
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    
    ll gcd(ll m, ll n)
    {
        return n == 0 ? m : gcd(n, m % n);
    }
    ll lcm(ll m, ll n)
    {
        return m * n / gcd(m, n);
    }
    
    int main()
    {
        int n;
        cin >> n;
        vector<int> a(n+1),pre(n+1),suf(n+2);
        pre[0] = ~0;
        suf[n + 1] = ~0;
        rep(i, 1, n)
            cin >> a[i];
        rep(i,1,n)
        {
            pre[i] = pre[i - 1] & (~a[i]);
        }
        dec(i, n, 1)
        {
            suf[i] = suf[i + 1] & (~a[i]);
        }
        int t0 = a[1] & suf[2];
        int t1 = a[n] & pre[n - 1];
        int pos=1;
        rep(i, 1, n-1)
        {
            if ((pre[i - 1] & a[i] & suf[i + 1]) > t0)
            {
                t0 = pre[i - 1] & a[i] & suf[i + 1];
                pos = i;
            }
        }
        if (t1 > t0)
            pos = n;
        swap(a[1], a[pos]);
        rep(i, 1, n)
            cout << a[i] << " ";
        return 0;
    }
  • 相关阅读:
    node.js
    js中文乱码问题
    238. Product of Array Except Self
    接下来要记得东西
    javascript 块内函数
    171. Excel Sheet Column Number
    Moore’s Voting Algorithm
    [ Python ] PIL
    [ Python ] KMP Algorithms
    房之事
  • 原文地址:https://www.cnblogs.com/dealer/p/12964689.html
Copyright © 2011-2022 走看看