CF675D.Tree Construction
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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.
- First element a1 becomes the root of the tree.
- Elements a2,a3,…,an are added one by one. To add element ai 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 ai 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 ai and becomes the corresponding child of the current node.
在编程课上,瓦西娅被布置了一道难题。然而,他不会编程,也无法在网上找到解决方案,因此他请求你帮忙。
给你一个由 n 个互不相同的整数组成的序列 a,该序列用于构建一棵二叉搜索树。以下是该构建过程的形式化描述:
- 第一个元素 a1 成为树的根节点。
- 元素 a2,a3,…,an 依次插入树中。插入元素 ai 时,需从根节点开始遍历树,并遵循以下规则:
- 将当前节点指针设为根节点。
- 若 ai 的值大于当前节点的值,则将当前节点的右子节点设为新的当前节点;否则,将当前节点的左子节点设为新的当前节点。
- 若在某一步中,所需子节点(左或右)不存在,则创建一个新节点,将其赋值为 ai,并作为当前节点的对应子节点。
输入格式
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 ai (1≤ai≤109) — the sequence a itself.
输入的第一行包含一个整数 n(2≤n≤100000)——序列 a 的长度。
第二行包含 n 个互不相同的整数 ai(1≤ai≤109)——序列 a 本身。
输出格式
Output n−1 integers. For all i>1 print the value written in the node that is the parent of the node with value ai in it.
输出 n−1 个整数。对于所有 i>1,输出值为 ai 的节点在树中的父节点所存储的值。
输入输出样例
输入#1
3 1 2 3
输出#1
1 2
输入#2
5 4 2 3 1 6
输出#2
4 2 2 4
输入解题思路,AI测评打分。不知道怎么写?