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 aa, consisting of nn distinct integers, that is used to construct the binary search tree. Below is the formal description of the construction process.

  1. First element a1a_1 becomes the root of the tree.
  2. Elements a2,a3,…,ana_2, a_3, \ldots, a_n are added one by one. To add element aia_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 aia_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 aia_i and becomes the corresponding child of the current node.

在编程课上,瓦西娅被布置了一道难题。然而,他不会编程,也无法在网上找到解决方案,因此他请求你帮忙。

给你一个由 nn 个互不相同的整数组成的序列 aa,该序列用于构建一棵二叉搜索树。以下是该构建过程的形式化描述:

  1. 第一个元素 a1a_1 成为树的根节点。
  2. 元素 a2,a3,…,ana_2, a_3, \ldots, a_n 依次插入树中。插入元素 aia_i 时,需从根节点开始遍历树,并遵循以下规则:
    1. 将当前节点指针设为根节点。
    2. 若 aia_i 的值大于当前节点的值,则将当前节点的右子节点设为新的当前节点;否则,将当前节点的左子节点设为新的当前节点。
    3. 若在某一步中,所需子节点(左或右)不存在,则创建一个新节点,将其赋值为 aia_i,并作为当前节点的对应子节点。

输入格式

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

The second line contains nn distinct integers aia_i (1≤ai≤1091 \leq a_i \leq 10^9) — the sequence aa itself.

输入的第一行包含一个整数 nn(2≤n≤100 0002 \leq n \leq 100\,000)——序列 aa 的长度。

第二行包含 nn 个互不相同的整数 aia_i(1≤ai≤1091 \leq a_i \leq 10^9)——序列 aa 本身。

输出格式

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

输出 n−1n - 1 个整数。对于所有 i>1i > 1,输出值为 aia_i 的节点在树中的父节点所存储的值。

输入输出样例

  • 输入#1

    3
    1 2 3

    输出#1

    1 2
  • 输入#2

    5
    4 2 3 1 6

    输出#2

    4 2 2 4

输入解题思路,AI测评打分。不知道怎么写?

首页