#include using namespace std; #define rep(i,n) for(int i=0;i<(int)(n);i++) #define ALL(v) v.begin(),v.end() typedef long long ll; template using V=vector; template using VV=V>; //B(n,V(n)) template struct BIT{ private: vector A; const int n; public: BIT(int _n) : A(_n+1,0), n(_n){} T sum(int i){ i++; T s=0; while(i>0){ s+=A[i]; i-=i&-i; } return s; } T sum(int i,int j){ return sum(j)-sum(i-1); } void add(int i,T x){ i++; while(i<=n){ A[i]+=x; i+=i&-i; } } void update(int i,T x){ T tmp=sum(i,i); if(tmp!=x) add(i,x-tmp); } int lower_bound(T w){ if(w<=0) return 0; if(sum(n-1)0;len=len>>1){ if(x+len>n; V P(n); map m; rep(i,n){ cin>>P[i]; m[P[i]]=i; } BIT bit(n); rep(i,n) bit.add(i,1); V used(n+1); priority_queue, greater > que; rep(i,n) que.push(i+1); V ANS; rep(i,n/2){ while(used[que.top()]) que.pop(); int p=que.top(); int a=bit.sum(m[p]); if(a==n-2*i){ que.pop(); while(used[que.top()]) que.pop(); int q=que.top(); que.pop(); int b=bit.sum(m[q]); int r=bit.lower_bound(b+1); ANS.push_back(q); ANS.push_back(P[r]); used[q]=1; used[P[r]]=1; bit.add(m[q],-1); bit.add(r,-1); que.push(p); } else{ int r=bit.lower_bound(a+1); ANS.push_back(p); ANS.push_back(P[r]); used[p]=1; used[P[r]]=1; bit.add(m[p],-1); bit.add(r,-1); que.pop(); } } rep(i,n){ if(i) cout<<" "; cout<