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.

 //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 ;
}

Codeforces675D(SummerTrainingDay06-J)的更多相关文章

  1. 【Java并发编程实战】-----“J.U.C”:CAS操作

    CAS,即Compare and Swap,中文翻译为"比较并交换". 对于JUC包中,CAS理论是实现整个java并发包的基石.从整体来看,concurrent包的实现示意图如下 ...

  2. 【Java并发编程实战】-----“J.U.C”:Exchanger

    前面介绍了三个同步辅助类:CyclicBarrier.Barrier.Phaser,这篇博客介绍最后一个:Exchanger.JDK API是这样介绍的:可以在对中对元素进行配对和交换的线程的同步点. ...

  3. 【Java并发编程实战】-----“J.U.C”:CountDownlatch

    上篇博文([Java并发编程实战]-----"J.U.C":CyclicBarrier)LZ介绍了CyclicBarrier.CyclicBarrier所描述的是"允许一 ...

  4. 【Java并发编程实战】-----“J.U.C”:CyclicBarrier

    在上篇博客([Java并发编程实战]-----"J.U.C":Semaphore)中,LZ介绍了Semaphore,下面LZ介绍CyclicBarrier.在JDK API中是这么 ...

  5. 【Java并发编程实战】-----“J.U.C”:ReentrantReadWriteLock

    ReentrantLock实现了标准的互斥操作,也就是说在某一时刻只有有一个线程持有锁.ReentrantLock采用这种独占的保守锁直接,在一定程度上减低了吞吐量.在这种情况下任何的"读/ ...

  6. JAVA并发编程J.U.C学习总结

    前言 学习了一段时间J.U.C,打算做个小结,个人感觉总结还是非常重要,要不然总感觉知识点零零散散的. 有错误也欢迎指正,大家共同进步: 另外,转载请注明链接,写篇文章不容易啊,http://www. ...

  7. Android Studio解决未识别Java文件(出现红J)问题

    1.问题:java文件出现了红J的问题,正常情况下应该是显示蓝色的C标识. 2.解决方案:切换到project视图下,找到app这个module里的build.gradle,在android结构里插入 ...

  8. //给定N个整数序列{A1,A2,A3...An},求函数f(i,j)=(k=i~j)Ak的求和

    //给定N个整数序列{A1,A2,A3...An},求函数f(i,j)=(k=i~j)Ak的求和 # include<stdio.h> void main() { ,sum1; ]={,- ...

  9. 面试题:给定数组a,找到最大的j-i, 使a[j]>a[i]

    第一种方法: 用两重循环对每对点都试一下,然后取最大值即可,时间复杂度为O(n2) #include <iostream> #include <algorithm> using ...

  10. 关于i和j

    算法课无聊随手写了段c代码,发现了个问题,就要下课了,先记一下 for(int i = 0; i < 100; i ++) for(int j = 0; j < 100000; j ++) ...

随机推荐

  1. Linux常用命令 - ls

    目录 Linux常用命令-ls 简介 ls -F ls -a ls -R ls -l 后记 Linux常用命令-ls 简介 ls主要用于显示文件和目录列表.作为最常见的Linux命令,大家应该都不会陌 ...

  2. 设置 Linux 下打印机的几种方式

    设置 Linux 下打印机的几种方式 一.使用 cups 进行设置 如若遇到 cups 也没有驱动的话可以前往 openprinting.org 找寻对应驱动. 二.前往 official 下载驱动 ...

  3. ssm中返回中文字符串时出现乱码?

    问题:返回json格式时,前端ajax请求,响应数据接收正常:     返回String时,响应数据是乱码? 解决:@RequestMapping注解中添加:produces = "text ...

  4. java filter过滤器及责任链设计模式

    什么是Filter? Filter属于sevlet规范,翻译为过滤器. Filter在web开发中有什么作用? 案例一:一个web站点只有用户登录才能继续访问该站点的资源,那么需要用户每次访问都判断是 ...

  5. POJ 2593

    #include <iostream> #include <stdio.h> using namespace std; int cmp ( const void *a , co ...

  6. 再学C/C++ 之 浅析gdb的-g选项

    浅析gdb的-g选项   准备 gdb是Linux下学习C/C++最常用的debug工具.要使用这一强大工具,必须在编译时加上-g选项,生成的目标文件才可以用gdb进行调试. 如,先创建一个test. ...

  7. Java 调用系统命令

    ProcessBuilder import java.io.File; import java.io.IOException; import java.io.InputStream; import j ...

  8. 为什么研发团队不适合量化KPI的绩效考核?

    研发团队(如果不是外包,不是机械性的活动)如果进行的是creative的有创造性的智力活动,那么应该不适合用量化KPI的绩效考核和激励,不应该用工时.bug数(难度大的bug可能多,测试人员可能没有经 ...

  9. php -- 特殊变量的三种输出

    ----- 020-3outputs.php ----- <!DOCTYPE html> <html> <head> <meta http-equiv=&qu ...

  10. php -- 正则替换

    ----- 019-regex_replace.php ----- <!DOCTYPE html> <html> <head> <meta http-equi ...