A10427 | Tree Construction
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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},...,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:
1. The pointer to the current node is set to the root.
2. 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.
3. 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.
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},...,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:
1. The pointer to the current node is set to the root.
2. 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.
3. 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.
输入格式
The first line of the input contains a single integer $n$ ( $2<=n<=100000$ ) — the length of the sequence $a$ .
The second line contains $n$ distinct integers $a_{i}$ ( $1<=a_{i}<=10^{9}$ ) — the sequence $a$ itself.
The second line contains $n$ distinct integers $a_{i}$ ( $1<=a_{i}<=10^{9}$ ) — the sequence $a$ itself.
输出格式
Output $n-1$ integers. For all $i>1$ print the value written in the node that is the parent of the node with value $a_{i}$ in it.
输入输出样例
输入 #1
3 1 2 3
输出 #1
1 2
输入 #2
5 4 2 3 1 6
输出 #2
4 2 2 4
Picture below represents the tree obtained in the first sample.

Picture below represents the tree obtained in the second sample.


Picture below represents the tree obtained in the second sample.

C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted