Codeforces Round #353 (Div. 2) D. Tree Construction 模拟
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 模拟的更多相关文章
- 数据结构 - Codeforces Round #353 (Div. 2) D. Tree Construction
Tree Construction Problem's Link ------------------------------------------------------------------- ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树
题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...
- Codeforces Round #353 (Div. 2) D. Tree Construction (二分,stl_set)
题目链接:http://codeforces.com/problemset/problem/675/D 给你一个如题的二叉树,让你求出每个节点的父节点是多少. 用set来存储每个数,遍历到a[i]的时 ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Codeforces Round #368 (Div. 2) B. Bakery (模拟)
Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- Codeforces Round #353 (Div. 2)
数学 A - Infinite Sequence 等差数列,公差是0的时候特判 #include <bits/stdc++.h> typedef long long ll; const i ...
- 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 ...
- 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 ...
随机推荐
- NVME SSD vs SATA SSD(转)
NVMe是个啥?未来SSD主流标准早知 关注固态硬盘的朋友应该对于这个词汇并不陌生,特别是今年NVMe也频繁出现在各大媒体文章中,随着高端SSD市场逐渐从SATA专项PCI-E时,以前的AHCI标准已 ...
- python基础-类的起源
Python中一切事物都是对象. class Foo(object): def __init__(self,name): self.name = name f = Foo("alex&quo ...
- 网络协议之UDP
前言 TCP协议在不可靠的网络环境上提供了可靠的通信通道,隐藏了大量的底层细节,使应用程序更加简洁.但有些应用并不需要这么高的可靠性,并不需要按序交付,而且TCP为了提高可靠性也增加了延时,在某些对延 ...
- 编写组件TComponent published $M+ 问题
报错如下: PUBLISHED caused RTTI ($M+) to be added to type 修改成下面这样之后: 解决问题 方法: 来自:http://www.cnblogs.com/ ...
- C++ 虚函数及重载、重定义、重写
#include<iostream> usingnamespace std; class BASE { public: BASE()=default; BASE(int publicVal ...
- css边框内凹圆角,解决优惠券的边框问题
关于css边框内凹圆角,找了好久才找到的 <html <head> <title>无标题页</title> <style> body{ backg ...
- 大数据统计分析平台之三、Kibana安装和使用
kibana安装 1.到官网下载kibana: cd /usr/local/software wget https://artifacts.elastic.co/downloads/kibana/ki ...
- C/C++经典面试题
1.指向数组的指针 和 指向数组首元素的指针 2018-03-07 昨天在牛客上看到这么一道C语言面试题,挺经典的,特来分享给大家. 程序如下,问输出结果 #include <stdio.h&g ...
- Linux--忘记MySQL密码的解决方法和输入mysqld_safe --skip-grant-tables &后无法进入MySQL的解决方法
https://blog.csdn.net/qq_35389417/article/details/78910974
- 站点的安全防范都是后端的职责?非也,Web前端安全同样不可忽视
前言 随着网络的快速普及,网络安全问题的受害者不再只是政府.企业等集体,每一个接触网络的普通人都有可能成为网络攻击的受害者.随着网络的普及,黑客进行网络攻击的手段越来也多,越来越复杂.以网站的攻击为例 ...