#pragma GCC optimize("Ofast") #include #define rep(i,n) for(ll i=0;i<(n);++i) #define ALL(x) x.begin(),x.end() #define BACK(x) x.rbegin(),x.rend() #define MOD1 1000000007 #define MOD2 998244353 #define MOD1_BASE 131 #define INF (LLONG_MAX / 2) #define FLOAT_ANS setprecision(30) #define TORAD(x) (x*acos(-1)/180.0) #define TODEG(x) (x*180/acos(-1)) #define GET_VALUENAME(value) # value using namespace std; using ll = long long; using LL = __int128_t; using ull = unsigned long long; template // T:重み using p_que = priority_queue,greater>; template bool chmin(T& a,T b){if(a>b){a=b;return true;}return false;} template bool chmax(T& a,T b){if(a0) {if(n&1)res=(res*(a%mod))%mod;a=((a%mod)*(a%mod))%mod;n>>=1;}return res;} template void RotateVec2(vector>&v){ll h=v.size();ll w=v[0].size();vector>t(w,vector(h));rep(i,h){rep(j,w){t[j][h-i-1]=v[i][j];}}v=t;} template bool InRange(T x, T mn, T mx){return (mn <= x && x <= mx);} template vector&merged(vector&a,vector&b) {vectorres;merge(a.begin(),a.end(),b.begin(),b.end(),back_inserter(res));return res;} struct UnionFind{ vectortree; UnionFind(ll x):tree(x, -1){} ll root(ll x){if(tree[x]<0) return x;return tree[x]=root(tree[x]);} bool same(ll x,ll y){return root(x)==root(y);} ll size(ll x){return -tree[root(x)];} void unite(ll x,ll y){x=root(x),y=root(y);if(x==y)return;if(size(x) struct SegTree{ ll n;T e;vectortree,lazy;functionf,add; SegTree(ll n_,functionf_,T e_=0,functionadd_=[](T next,T old){return next;}):e(e_),f(f_),add(add_){ ll x=1; while(x class modint { using u64 = std::uint_fast64_t; public: u64 a; constexpr modint(const u64 x = 0) noexcept : a(((x % Modulus)+Modulus)%Modulus) {} constexpr u64 &value() noexcept { return a; } constexpr const u64 &value() const noexcept { return a; } constexpr modint operator+(const modint rhs) const noexcept {return modint(*this) += rhs;} constexpr modint operator-(const modint rhs) const noexcept {return modint(*this) -= rhs;} constexpr modint operator*(const modint rhs) const noexcept {return modint(*this) *= rhs;} constexpr modint operator/(const modint rhs) const noexcept {return modint(*this) /= rhs;} constexpr modint &operator+=(const modint rhs) noexcept {a += rhs.a;if (a >= Modulus) {a -= Modulus;}return *this;} constexpr modint &operator-=(const modint rhs) noexcept {if (a < rhs.a) {a += Modulus;}a -= rhs.a;return *this;} constexpr modint &operator*=(const modint rhs) noexcept {a = a * rhs.a % Modulus;return *this;} constexpr modint &operator/=(modint rhs) noexcept { u64 exp = Modulus - 2; while (exp) { if (exp % 2) { *this *= rhs; } rhs *= rhs; exp /= 2; } return *this; } constexpr modint &operator=(u64 x){ a = x % Modulus; return *this; } }; template struct Vector2D { T x, y; Vector2D():x(0),y(0) {} Vector2D(T x_, T y_):x(x_),y(y_) {} double length() const { return sqrt((double)x*x+y*y); }; T lengthp() const { return x*x+y*y; }; bool inrange(const Vector2D a, const Vector2D b) { return (InRange(x, a.x, b.x) and InRange(y, a.y, b.y)); } Vector2D yx() { return Vector2D{ y, x }; } Vector2D operator-(const Vector2D a) const { return Vector2D(*this) -= a; } Vector2D operator+(const Vector2D a) const { return Vector2D(*this) += a; } T operator*(const Vector2D a) const { return x*a.x+y*a.y; } Vector2D operator*(const T a) const { return Vector2D(*this) *= a; } Vector2D operator/(const T a) const { return Vector2D(*this) /= a; } Vector2D &operator+=(const Vector2D a) { x += a.x; y += a.y; return *this; } Vector2D &operator-=(const Vector2D a) { x -= a.x; y -= a.y; return *this; } Vector2D &operator-=(const T a) { x -= a; y -= a; return *this; } Vector2D &operator*=(const T a) { x *= a; y *= a; return *this; } Vector2D &operator/=(const T a) { x /= a; y /= a; return *this; } friend ostream& operator<< (ostream& stream, const Vector2D<>& x); bool operator==(const Vector2D a) const { return (x==a.x and y==a.y); } bool operator!=(const Vector2D a) const { return not (x==a.x and y==a.y); } bool operator>(const Vector2D a) const { return a < *this; } bool operator<(const Vector2D a) const { return make_pair(x,y) < make_pair(a.x, a.y); // return x*a.y < y*a.x; } }; ostream& operator<< (ostream& stream, const Vector2D& x) { string s = "(" + to_string(x.x) + ", " + to_string(x.y) + ")"; stream << s; return stream; } ll popcount(ll x) { ll res = 0; while(x) {res+=x%2;x>>=1;} return res; } // debug kit void print() { cout << endl; } template void print_(vectorx) { for(auto i : x) cout << i << " "; } template void print_(T x) { cout << x << " "; } #ifdef ONLINE_JUDGE template void print(T head, Args... args) {} template void debug(T value) {} #else template void print(T head, Args... args) { print_(head); print(args...); } template void debug(T value) { print((string)"\""+GET_VALUENAME(value)+"\": ", value); } #endif // MAIN PROGRAM ------------ using mint = modint; using Vec2 = Vector2D; const Vec2 Angle[] = {{0,1}, {0,-1}, {-1,0}, {1,0}}; struct Node { ll prev, next, value; bool valid = true; }; int main() { ll n; cin >> n; vectorp(n+2); vectorinv(n); rep(i, n) { cin >> p[i].value; --p[i].value; p[i].next = i+1; p[i].prev = i-1; inv[p[i].value] = i; } p[0].prev = n+1; vectorq; functiongetNext = [&](ll i) { ll next = p[i].next; if (p[next].valid) return next; return p[i].next = getNext(p[i].next); }; functiongetPrev = [&](ll i) { ll prev = p[i].prev; if (p[prev].valid) return prev; return p[i].prev = getPrev(p[i].prev); }; // for (auto i : p) cout << i.value << " "; rep(i, n) { ll idx = inv[i]; if (not p[inv[i]].valid) continue; if (getNext(idx) == n) continue; // cout << i + 1 << " " << inv[i] << endl; Node &x = p[idx], &y = p[getNext(idx)]; Node &prev = p[getPrev(idx)], &next = p[getNext(getNext(idx))]; x.valid = y.valid = false; q.push_back(x.value+1); q.push_back(y.value+1); prev.next = y.next; next.prev = x.prev; } for (auto i : q) cout << i << " "; cout << endl; }