D. Tree Construction time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

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 a1 becomes the root of the tree.
  2. 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:
    1. The pointer to the current node is set to the root.
    2. 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.
    3. 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.
Input

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

The second line contains n distinct integers ai (1 ≤ ai ≤ 109) — the sequence a itself.

Output

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.

Examples input
3
1 2 3
output
1 2
input
5
4 2 3 1 6
output
4 2 2 4
Note

Picture below represents the tree obtained in the first sample.

Picture below represents the tree obtained in the second sample.



Source

D. Tree Construction



My Solution

这个  construct the binary search tree 是 按 照 输 入 顺 序 构 造 的,每次从根部遍历按照二叉搜索树原理去找值然后把节点加上去

traverse the tree starting from the root  当时查单词的时候 traverse
只看到了"旋转" 然后就懵了, 结果还有遍历的意思,而且根据句子翻译也是 遍历 (┬_┬)  这里不用rotate()旋转


看了题解才注意到这个,然后才搞明白 ^_^


所以每次插入,只要考虑已经出现(在树上)的比它大而且在隔壁iter, 比它小而且在隔壁--iter,的两个位置

这时--iter 可能是iter的左节点 或者 iter是--iter的右节点 这2种情况之一,

所以先判断,iter左节点是否有值   //先判断--iter的右边是否有值也可以,效果一样。反正2种情况中的一种

如果没有值就放上去

如果有值就插在--iter的右边


#include <iostream>
#include <cstdio>
#include <set>
#include <map>
using namespace std;

set<int> valset;
map<int, int> L, R;                //这里数字比较离散,用map好,而且数组好像也开不下


int main()
{
    int n, val;
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        scanf("%d", &val);
        if(i == 0) valset.insert(val);
        else{
            auto iter = valset.lower_bound(val);    //返回, 一个迭代器, 这里插入val, 顺序不变(比它大的那些往后推)
            if(iter == valset.end()) R[*(--iter)] = val;
            else{
                if(!L[*iter]) L[*iter] = val;      //优先作为比它大的且相邻的数的左子节点
                else R[*(--iter)] = val;             //如果什么的左节点非空,说明这个点比val小且相邻的点连在L[*iter] 上了
            }
            if(i == 1)printf("%d", *iter);
            else printf(" %d", *iter);
            valset.insert(val);
        }
    }
    return 0;
}

Thank you!

                                                                                                                                               ------from ProLights

更多推荐

Codeforces Round #353 (Div. 2) D. Tree Construction __ Binary Search Tree