CF675D.Tree Construction

普及/提高-

通过率:0%

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},...,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:
  3. The pointer to the current node is set to the root.
  4. 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.
  5. 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.

输入格式

The first line of the input contains a single integer nn ( 2<=n<=1000002<=n<=100000 ) — the length of the sequence aa .

The second line contains nn distinct integers aia_{i} ( 1<=ai<=1091<=a_{i}<=10^{9} ) — the sequence aa itself.

输出格式

Output n1n-1 integers. For all i>1 print the value written in the node that is the parent of the node with value aia_{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.

首页