/* -*- coding: utf-8 -*- * * 2740.cc: No.2740 Old Maid - yukicoder */ #include #include #include #include using namespace std; /* constant */ const int MAX_N = 200000; /* typedef */ typedef pair pii; typedef set spii; struct Node { int v, i; Node *prv, *nxt; Node(): v(), i(), prv(), nxt() {} Node(int _v, int _i): v(_v), i(_i), prv(), nxt() {} void remove() { if (prv != nullptr) prv->nxt = nxt; if (nxt != nullptr) nxt->prv = prv; prv = nxt = nullptr; } }; /* global variables */ int ps[MAX_N], qs[MAX_N]; Node as[MAX_N]; /* subroutines */ /* main */ int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", ps + i); spii ss; for (int i = 0; i < n; i++) { as[i].v = ps[i], as[i].i = i; if (i > 0) as[i].prv = &as[i - 1]; if (i + 1 < n) as[i].nxt = &as[i + 1]; ss.insert({ps[i], i}); } int m = 0; for (int i = 0; i < n; i += 2) { int j = ss.begin()->second; ss.erase(ss.begin()); if (as[j].nxt == nullptr) { j = ss.begin()->second; ss.erase(ss.begin()); } ss.erase({as[j].nxt->v, as[j].nxt->i}); qs[m++] = as[j].v, qs[m++] = as[j].nxt->v; as[j].nxt->remove(); as[j].remove(); } for (int i = 0; i < n; i++) printf("%d%c", qs[i], (i + 1 < n) ? ' ' : '\n'); return 0; }