Codeforces Round 353 (Div. 2)
D
Tree Construction
Codeforces
1800
原题
← CE →
翻译中…

During the programming classes Vasya was assigned a difficult problem. However, he doesn't know how to code and was unable to find the solution in the Internet, so he asks you to help.

翻译中…

You are given a sequence $a$, consisting of $n$ distinct integers, that is used to construct the binary search tree. Below is the formal description of the construction process.

翻译中…
  1. First element $a_1$ becomes the root of the tree.
  2. Elements $a_2, a_3, \ldots, a_n$ are added one by one. To add element $a_i$ one needs to traverse the tree starting from the root and using the following rules:
  • The pointer to the current node is set to the root.
    • If $a_i$ is greater than the value in the current node, then its right child becomes the current node. Otherwise, the left child of the current node becomes the new current node.
    • If at some point there is no required child, the new node is created, it is assigned value $a_i$ and becomes the corresponding child of the current node.
翻译中…

Input

翻译中…

The first line of the input contains a single integer $n$ ($2 \leq n \leq 100\,000$) — the length of the sequence $a$.

翻译中…

The second line contains $n$ distinct integers $a_i$ ($1 \leq a_i \leq 10^9$) — the sequence $a$ itself.

翻译中…

Output

翻译中…

Output $n - 1$ integers. For all $i \gt 1$ print the value written in the node that is the parent of the node with value $a_i$ in it.

翻译中…

样例

Input #1
3
1 2 3
Output #1
1 2
InputCopy OutputCopy
Input #2
5
4 2 3 1 6
Output #2
4 2 2 4
InputCopy OutputCopy
题目信息
题号
675D
时间限制
2 s
内存限制
256 MB
标签
数据结构
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。