#include "partition.h"
#include<iostream>
#include<vector>
#include <map>
#include <algorithm>
using namespace std;
int long long x,nr,n,p,i1,i2,a[1100000],b[1100000],ok;
struct vl
{
    int val,p;
}v[1100000];
bool ord (vl a,vl b)
{
    return a.val<b.val;
}

vector<int> solve(vector<int> A)
{
    vector<int>sol;
    n=p=0;
    sol.clear();
    n=A.size();
    while (A.size())
    {
        x=A.back();
        A.pop_back();
        nr=0;
        while (x) nr++,x=x/2;
        p++;
        v[p].val=nr;
        v[p].p=n-p+1;
    }
    n=p;
    sort (v+1,v+n+1,ord);
    i1=i2=0;
    ok=0;
    for (int i=1; i<=n; i++)
    if (v[i].val==v[i+1].val && i<n) a[++i1]=v[i].p,b[++i2]=v[i+1].p,i++;
    else ;

    for (int i=1; i<=n; i++)
    if (v[i].val==v[i+1].val && i<n) i++;
    else
    {
        if (ok==0) a[++i1]=v[i].p,ok=1;
        else b[++i2]=v[i].p,ok=0;
    }

    if (b[i2]>a[i1]) swap (b[i2],a[i1]);

    for (int i=1; i<=i1; i++)
    sol.push_back(a[i]-1);

    for (int i=1; i<=i2; i++)
    sol.push_back(b[i]-1);

    return sol;
}
/*
int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin >> t ;
    for (int test=1; test<=t; test++) {
        int n;
        cin >> n ;
        vector <int> a(n);
        for (int i=0; i<n; i++) {
            cin >> a[i] ;
        }

        for (auto ind : solve(a)) {
            cout << ind << " ";
        }
        cout << endl ;
    }
    cout << endl ;
    return 0;
}

*/
