【題解】Codeforces 1300C. Anu Has a Function

【題目敘述】http://codeforces.com/contest/1300/problem/C

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
 
int n, a[100005], check[32], ans[32], I;
 
int main() {
    cin >> n;
    memset(ans, -1, sizeof(ans));
    for (int i = 0; i < n; i++){
        cin >> a[i];
        for (int j = 0; j < 32; j++){
            if ((1<<j) > a[i]) break;
            if (check[j] == 0 && (a[i] & (1<<j))){
                if (ans[j] != -1) check[j] = 1;
                else ans[j] = i;
            }
        }
    }
    I = -1;
    for (int i = 31; i >= 0; i--){
        if (check[i] == 0 && ans[i] != -1){
            I = ans[i];
            break;
        }
    }
    if (I != -1){
        cout << a[I];
        for (int i = 0; i < I; i++){
            cout << " " << a[i];
        }
        for (int i = I+1; i < n; i++){
            cout << " " << a[i];
        }
    }
    else{
        cout << a[0];
        for (int i = 1; i < n; i++){
            cout << " " << a[i];
        }
    }
    cout << "\n";
}
分享本文 Share with friends