#include "partition.h"
#include<iostream>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
bool marked[(1<<25)+1];
int cnt[(1<<25)+1];
std::vector<int> solve(std::vector<int> A)
{
    vector<int>ans;
    vector<int>other;
    vector<int>B=A;
    /*vector<pair<int,int>>v;
    for(int i=0;i<A.size();i++)
    {
        v.push_back({A[i],i});
    }
    sort(v.begin(),v.end());
    for(int i=0;i<v.size();i+=2)
    {
        ans.push_back(v[i].second);
        other.push_back(v[i+1].second);
    }*/


    for(int i=0;i<A.size();i++)
    {
        cnt[A[i]]++;
    }

    for(int i=0;i<A.size();i++)
    {
        if(cnt[A[i]]%2==0)marked[A[i]]=1;
    }

    vector<pair<int,int>>left;
    for(int i=0;i<A.size();i++)
    {
        if(marked[A[i]]==0 && cnt[A[i]]==1)
        {
            cnt[A[i]]--;
            left.push_back({A[i],i});
            continue;
        }

        if(cnt[A[i]]==0)continue;

        if(cnt[A[i]]%2==0)ans.push_back(i);
        else other.push_back(i);

        cnt[A[i]]--;
    }

    for(int i=0;i<A.size();i++){marked[A[i]]=0;cnt[A[i]]=0;}
    sort(left.begin(),left.end());

    int n=left.size();

    if(n!=0){
    if(n%2==0){
    for(int i=1;i<n/2;i++)
    {
        ans.push_back(left[i-1].second);
    }
    ans.push_back(left[n-1].second);
    for(int i=n/2;i<n;i++)
    {
        other.push_back(left[i-1].second);
    }}
    else
    {
        for(int i=1;i<=n/2;i++)
        {
            ans.push_back(left[i-1].second);
        }
        ans.push_back(left[n-1].second);
        for(int i=n/2+1;i<n;i++)
        {
            other.push_back(left[i-1].second);
        }
    }}


    for(int x:other)ans.push_back(x);
    return ans;
}
