CodeForces - 675D Tree Construction (set&数据结构)
2016-05-31 19:10
627 查看
CodeForces - 675D
Tree Construction
Submit
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.
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
Input
Output
Input
Output
Sample Output
Hint
Source
Codeforces Round #353 (Div. 2)
//题意:
给你n个节点,第一个节点作为根节点,接下来的如果节点值>他的父节点,就将他练到他的右边,否则练到左边,输出从2--n节点的父节点的值。
//思路:
set模拟
Tree Construction
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.
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
Input
3 1 2 3
Output
1 2
Input
5 4 2 3 1 6
Output
4 2 2 4
Sample Output
Hint
Source
Codeforces Round #353 (Div. 2)
//题意:
给你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; }
相关文章推荐
- 数据结构学习-数组和广义表
- 数据结构学习-递归(背包问题)
- 大话数据结构:线性表(3)
- hdu 2923 floyd+map容器
- hdu 1217 floyd+map容器
- hdu 2112 floyd+map容器
- hdu 1548 最短路
- hdu 2680 最短路
- hdu 1166线段树入门
- ACM 数据结构 线段树 HDU 1540 Tunnel Warfare
- 线段树入门
- OpenResty学习笔记(一)数据结构与控制流
- POJ 1611 The Suspects( 并查集)
- ACM 数据结构 线段树 HDU 1698Just a Hook
- ACM 数据结构 线段数 HDU 1754 I Hate It
- ACM 数据结构 线段数 HDU 1166 敌兵布阵
- 白话篇:利用二叉树先序/中序/后序确定二叉树求法分析
- java 数据结构之单链表
- Intersection of Two Linked Lists
- 遍历二叉树