CodeForces - 675D Tree Construction (set&数据结构)

原创 2016年05月31日 19:10:46
CodeForces - 675D
Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u

Status

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 ndistinct 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.

Sample Input

Input
3
1 2 3
Output
1 2
Input
5
4 2 3 1 6
Output
4 2 2 4

Sample Output

Hint

Source

//题意:
给你n个节点,第一个节点作为根节点,接下来的如果节点值>他的父节点,就将他练到他的右边,否则练到左边,输出从2--n节点的父节点的值。

//思路:
set模拟
#include<stdio.h>
#include<string.h>
#include<set>
#include<map>
#include<algorithm>
#include<iostream>
using namespace std;
set<int>t;
set<int>::iterator it;
set<int>::iterator it1;
set<int>::iterator it2;
map<int,int>pos;
int main()
{
	int n,x;	
	while(scanf("%d",&n)!=EOF)
	{
		t.clear();
		int T=1;
		scanf("%d",&x);
		t.insert(0);t.insert(x);
		pos[x]=T++;
		while(T<=n)
		{
			scanf("%d",&x);
			it=t.lower_bound(x);			
			it1=it--;it2=it;
			if(pos[*it1]<pos[*it2])
				printf("%d ",*it2);				
			else
				printf("%d ",*it1);
			t.insert(x);
			pos[x]=T++;
		} 
	}
	return 0;
} 


相关文章推荐

Codeforces 675D Tree Construction【思维+set】

D. Tree Construction time limit per test 2 seconds memory limit per test 256 megabytes in...

codeforces-675D Tree Construction

大致题意:输入n个不相同的数,依次插入bst中,输出最后每个数对应节点的父亲思路一:bst的中序遍历是有序的,所以每一棵子树对应有序序列的一个区间。先将原序列排序,按插入顺序取没有处理的节点,找到它在...

【CF 675D】 Tree Construction(离线二分+左右指针)

【CF 675D】 Tree Construction(离线二分+左右指针) D. Tree Construction time limit per t...

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

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

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

During the programming classes Vasya was assigned a difficult problem. However, he doesn't know how ...

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

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

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

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

K-D tree 数据结构

首先来一个问题:     给定平面上一个点集 E ,还有一个定点 V ,怎么在一群点中找出一个点 U,使得 V 与 U 的距离最近(欧几里得距离)? 当然,我们能够想到一种做法:枚举 E 中所有的...

高级数据结构之K-D-TREE

k-d-tree(即k-dimensional tree)是一棵形如二叉树的一种非常重要的空间划分数据结构,尤其在多维数据访问中有重要应用。特别是机器学习中的kNN(k近邻)最常用到的搜索算法就是以k...

第三周周赛——基础数据结构结业场(坚持就会有AK,题目出自codeforces 633C,633D,631B,651A,651C以及poj1577)

A题: A题题目链接 题目描述: Fibonacci-ish TimeLimit:3000MS  MemoryLimit:512MB 64-bit integer IO format:%I...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:CodeForces - 675D Tree Construction (set&数据结构)
举报原因:
原因补充:

(最多只允许输入30个字)