Language: C++

#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<cmath>
#include<complex>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<ctype.h>
#include<cwchar>
#include<cwctype>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<math.h>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<stdio.h>
#include<stdlib.h>
#include<streambuf>
#include<string.h>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
#define x first
#define y second
#define m_p make_pair
using namespace std;
const int sz=262144;
inline int mini(int a,int b){
	return (a>b)?b:a;
}
inline void gtmn(int &a,int b){
	if (a>b) a=b;
}
int odd[524299],eve[524299];
void upd(int *a,int i){
	a[i]=mini(a[i<<1],a[i<<1|1]);
}
void update(int *a,int i){
	i>>=1;
	while(i){
		upd(a,i);
		i>>=1;
	}
}
int query(int *a,int l,int r){
	int res=100000000;
	while(l<=r){
		if (l&1){
			gtmn(res,a[l++]);
		}
		if (!(r&1)){
			gtmn(res,a[r--]);
		}
		l>>=1;
		r>>=1;
	}
	return res;
}
set<pair<int,pair<int,int> > > s;
int inv[200010],init[200010];
int main(void){
	memset(odd,55,sizeof(odd));
	memset(eve,55,sizeof(eve));
	int n;
	scanf("%d",&n);
	for (int i=1;i<=n;i++){
		scanf("%d",&init[i]);
		inv[init[i]]=i;
		if (i&1){
			odd[i+sz]=init[i];
		}
		else{
			eve[i+sz]=init[i];
		}
	}
	for (int i=sz-1;i;i--){
		upd(odd,i);
		upd(eve,i);
	}
	s.insert(m_p(query(odd,1+sz,n+sz),m_p(1,n)));
	for (int _=1;_<=(n>>1);_++){
		assert(!s.empty());
		int mn=s.begin()->x,hd=s.begin()->y.x,tl=s.begin()->y.y;
		s.erase(s.begin());
		if (hd&1){
			if (inv[mn]==hd){
				int t2=inv[query(eve,hd+sz,tl+sz)];
				printf("%d %d ",mn,init[t2]);
				if (t2>hd+1){
					s.insert(m_p(query(eve,hd+sz+1,t2+sz-1),m_p(hd+1,t2-1)));
				}
				if (tl>t2){
					s.insert(m_p(query(odd,t2+sz+1,tl+sz),m_p(t2+1,tl)));
				}
			}
			else{
				int t2=inv[mn];
				printf("%d %d ",mn,init[tl]);
				if (t2>hd){
					s.insert(m_p(query(odd,hd+sz,t2+sz-1),m_p(hd,t2-1)));
				}
				if (t2<tl-1){
					s.insert(m_p(query(eve,t2+sz+1,tl+sz-1),m_p(t2+1,tl-1)));
				}
			}
		}
		else{
			if (inv[mn]==hd){
				int t2=inv[query(odd,hd+sz,tl+sz)];
				printf("%d %d ",mn,init[t2]);
				if (t2>hd+1){
					s.insert(m_p(query(odd,hd+sz+1,t2+sz-1),m_p(hd+1,t2-1)));
				}
				if (tl>t2){
					s.insert(m_p(query(eve,t2+sz+1,tl+sz),m_p(t2+1,tl)));
				}
			}
			else{
				int t2=inv[mn];
				printf( %d %d ",mn,init[tl]);
				if (t2>hd){
					s.insert(m_p(query(eve,hd+sz,t2+sz-1),m_p(hd,t2-1)));
				}
				if (t2<tl-1){
					s.insert(m_p(query(odd,t2+sz+1,tl+sz-1),m_p(t2+1,tl-1)));
				}
			}
		}
	}
	assert(s.empty());
	return 0;
}

Compiler output:
a.cc:153:47: warning: missing terminating " character
  153 |                                 printf( %d %d ",mn,init[tl]);
      |                                               ^
a.cc:153:47: error: missing terminating " character
  153 |                                 printf( %d %d ",mn,init[tl]);
      |                                               ^~~~~~~~~~~~~~~
a.cc: In function 'int main()':
a.cc:153:41: error: expected primary-expression before '%' token
  153 |                                 printf( %d %d ",mn,init[tl]);
      |                                         ^
a.cc:153:42: error: 'd' was not declared in this scope; did you mean 'hd'?
  153 |                                 printf( %d %d ",mn,init[tl]);
      |                                          ^
      |                                          hd