Codeforces Round #618 (Div. 2) C.Anu Has a Function


F ( X , Y ) = ( X | Y ) - Y bits in binary split found for bit i

If x, y are both i-1, i of the function result bit is 0

When the i-th bit x 0, y i-th bit is 1, the function result is bit i 0

I bit i if x is 1, y i-th bit is 0, the function result is a bit i

 For F ( F ( ... F ( F ( A 1 , A 2 ) , A . 3 ) , ... A n- - 1 ) , A n- ) no matter how the order of the row number i just 1 bit linear function ≥2 n after the i-th calculation result bit is 0 

prove:

 

1. If a1 i-th bit is 0 then no matter how the n-th row of function calculation result bit i is 0

2. When the i-th bit a1 encountered 1 second after the i-th bit number 1 as a function of the results of the i-th bit becomes 0 after first case

Therefore, the final answer depends only on the first number, the first number appearing only once in the number n of the bit

So to answer the most, from the beginning greedy high, as long as this bit appears only once in the loop exits because of back and apparently did not affect the position of the great,

Reflection: 1. For questions involving bit computing and selecting the maximum value, usually greedy from high to low, should be decisive split bits  

           It should not be only in the order of a few years, but also according to bits

         2. The function is equivalent to xx & y iteration time made a mistake Shabi f (f (x, y), z) = xx & y-x & y & z the fact that no matter how the results are only two iterated function

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int a[N];
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];
    int ans=0;
    for(int i=30;i>=0;i--){
        int cnt=0,pos=0;
        for(int j=1;j<=n;j++)
         if(a[j]&(1<<i)){
             cnt++;
             pos=j;
         }
         if(cnt==1){
         ans=pos;
         break;}
    }
    if(ans==0)for(int i=1;i<=n;i++)cout<<a[i]<<' ';
    else {
        cout<<a[ans]<<' ';
        for(int i=1;i<=n;i++)
         if(ans!=i)cout<<a[i]<<' ';
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/wyh447154317/p/12289656.html