Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Code : XOUR

#include <bits/stdc++.h>
using namespace std;

void solve(vector<int> &a) {
    int n = a.size();

    map<int, vector<int>> cc;
    for(int i = 0; i < n; i++) {
        cc[a[i] >> 2].push_back(a[i]);
    }

    for(auto &kv : cc) {
        auto &vec = kv.second;
        sort(vec.begin(), vec.end(), greater<int>());
    }

    for(int i = 0; i < n; i++) {
        auto &vec = cc[a[i] >> 2];
        cout << vec.back() << " ";
        vec.pop_back();
    }
    cout << "\n";
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t; cin >> t;
    for(int zz = 0; zz < t; zz++) {
        int n; cin >> n;
        vector<int> a(n);
        for(int i = 0; i < n; i++) {
            cin >> a[i];
        }
        solve(a);
    }
    return 0;
}