首页 技术 正文
技术 2022年11月19日
0 收藏 661 点赞 3,737 浏览 1881 个字

D. Tree Construction

time limit per test:2 secondsmemory limit per test:256 megabytesinput:standard inputoutput: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.

Codeforces675D(SummerTrainingDay06-J)

Picture below represents the tree obtained in the second sample.

Codeforces675D(SummerTrainingDay06-J)

 //2017-09-05
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
#include <set> using namespace std; int n;
map<int, int> pos;
set<int> st;
set<int>::iterator iter, it; int main()
{
std::ios::sync_with_stdio(false);
cin.tie();
freopen("inputJ.txt", "r", stdin);
while(cin>>n){
st.clear();
pos.clear();
int a;
cin>>a;
st.insert(a);
st.insert();
pos[a] = ;
for(int i = ; i <= n; i++){
cin>>a;
iter = st.lower_bound(a);//返回第一个大于等于a的数
it = iter;
it--;//比a小的最大的数
if(pos[*it] > pos[*iter])
cout<<*it<<" ";
else cout<<*iter<<" ";
pos[a] = i;
st.insert(a);
}
cout<<endl;
} return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,909
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,434
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,249
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,060
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,692
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,730