D. Tree Construction

题目连接:

http://www.codeforces.com/contest/675/problem/D

Description

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.

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.

Sample Input

3

1 2 3

Sample Output

1 2

Hint

题意

给你平衡树的节点,每个节点都依次插进去的,问你每个节点插进去之后,他的父亲是谁

题解:

没必要真的写个平衡树,直接用stl模拟就好了……

代码

#include<bits/stdc++.h>
using namespace std;
set<int> s;
map<int,int> ls,rs;
int x;
int main()
{
int n;
scanf("%d",&n);
scanf("%d",&x);
s.insert(x);
for(int i=1;i<n;i++)
{
scanf("%d",&x);
auto it=s.lower_bound(x);
if(it==s.end())
{
it--;
rs[*it]=x;
printf("%d ",*it);
}
else if(ls[*it]==0)
{
ls[*it]=x;
printf("%d ",*it);
}
else
{
it--;
rs[*it]=x;
printf("%d ",*it);
}
s.insert(x);
}
printf("\n");
}

Codeforces Round #353 (Div. 2) D. Tree Construction 模拟的更多相关文章

  1. 数据结构 - Codeforces Round #353 (Div. 2) D. Tree Construction

    Tree Construction Problem's Link ------------------------------------------------------------------- ...

  2. Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树

    题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...

  3. Codeforces Round #353 (Div. 2) D. Tree Construction (二分,stl_set)

    题目链接:http://codeforces.com/problemset/problem/675/D 给你一个如题的二叉树,让你求出每个节点的父节点是多少. 用set来存储每个数,遍历到a[i]的时 ...

  4. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  5. Codeforces Round #368 (Div. 2) B. Bakery (模拟)

    Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...

  6. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  7. Codeforces Round #353 (Div. 2)

    数学 A - Infinite Sequence 等差数列,公差是0的时候特判 #include <bits/stdc++.h> typedef long long ll; const i ...

  8. Codeforces Round #284 (Div. 2)A B C 模拟 数学

    A. Watching a movie time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. Codeforces Round #285 (Div. 2) A B C 模拟 stl 拓扑排序

    A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

随机推荐

  1. python numpy数组中的复制问题

    vector = numpy.array([5, 10, 15, 20]) equal_to_ten_or_five = (vector == 10) | (vector == 5) vector[e ...

  2. 『实践』百度地图给map添加右键菜单(判断是否为marker)

      var map; var s;//经度 var w;//纬度 $(document).ready(function(){ $(".mune").load("jsp/c ...

  3. 使用离线包部署kubernetes 1.9.0、kubernetes-dashboard 1.8

    =============================================== 2018/3/22_第2次修改                       ccb_warlock 更新 ...

  4. poj1067

    题意:有两堆石子,两人轮流取,每次可以取一堆中的任意个,或两堆中取相同多个.谁先取光所有堆谁赢.问先手能否获胜. 分析:威佐夫博弈,如果是奇异态则先手输,否则先手赢.直接套用公式判断是否为奇异态,设第 ...

  5. 【前端vue开发】vue项目使用sass less扩展语言所要安装的依赖

    1.创建一个基于 webpack 模板的新项目 $ vue init webpack myvue 2.在当前目录下,安装依赖 $ cd myvue $ npm install 3.安装sass的依赖包 ...

  6. Python3 出现'ascii' codec can't encode characters问题

    当使用urllib.request.urlopen打开包含中文的链接时报错: from urllib import request url = 'https://baike.baidu.com/ite ...

  7. acm专题---KMP模板

    KMP的子串长n,模式串长m,复杂度o(m+n),朴素做法的复杂度o((n-m+1)*m) 觉得大话数据结果上面这个讲得特别好 改进版本的KMP leetcode 28. Implement strS ...

  8. java基础72 junit单元测试

    1.junit要注意的细节 1.如果junit测试一个方法,在junit窗口上显示绿色代表测试成功:如果显示红条,则代表测试方法出现异常不通过.    2.如果点击方法名.包名.类名.工程名运行jun ...

  9. matlab随笔

    主要是记录一些函数.(博客园的一些操作实在是太不方便了) cat函数:http://blog.sina.com.cn/s/blog_6b7dfd9d0100mnz7.html 联结两个数组 magic ...

  10. Python模块Pygame安装

    一.使用pip安装Python包 大多数较新的Python版本都自带pip,因此首先可检查系统是否已经安装了pip.在Python3中,pip有时被称为pip3. 1.在Linux和OS X系统中检查 ...