Summer Training day6 codeforces 675D 二叉搜索树
来源:互联网 发布:一口价域名 编辑:程序博客网 时间:2024/05/18 03:34
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.
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 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.
31 2 3
1 2
54 2 3 1 6
4 2 2 4
Picture below represents the tree obtained in the first sample.
Picture below represents the tree obtained in the second sample.
题意:按要求构造一颗二叉搜索树,然后输出每个结点的父节点是谁。
我们用set维护一个有序序列,对于新插入的元素a,用lower_bound找到一个位置,如果这个位置是set.begin()的话,那么父节点就是当前找到位置的元素。
如果找到的位置不为set.begin()的话,那么现在有两种情况,可能是前一个位置的右孩子,也有可能是当前位置的做左孩子。
我们用一个数组right[MAXN]代表的是该节点是否有右孩子。我们先判断一下前一个位置有没有右孩子,没有的话就认为它是前一个位置的右孩子,否则的话就是当前位置的左孩子。
代码:
#include <iostream>#include <cstdio>#include <set>#include <map>using namespace std;const int MAXN = 1e5+10;map<int,int> rights;int n;int main(){set<int> st;scanf("%d",&n);int tmp; scanf("%d",&tmp);st.insert(tmp);for(int i = 2;i <= n;i++){int tmp;scanf("%d",&tmp);set<int> ::iterator it = st.lower_bound(tmp);if(it == st.begin()){printf("%d ",*it);}else{int pre = *(--it);if(rights[pre] == 0){rights[pre] = 1;printf("%d ",pre);}else{printf("%d ",*(++it));}}st.insert(tmp);}return 0;}/*1010 5 7 9 2 1 4 8 3 6 */
- Summer Training day6 codeforces 675D 二叉搜索树
- Summer Training day6 codeforces 593D LCA+并查集
- Summer Training day6 codeforces633G 线段树、bitset
- Summer Training day6 coseforces339D 线段树、位操作
- Summer Training day6 codeforces240F 线段树、回文串
- Summer Training day6 codeforces343D dfs序、线段树
- BUPT-SUMMER-TRAINING-搜索
- ecjtu-summer training #2 D
- ecjtu-summer training #5 D
- Summer training day6 codeforces19E 树链剖分,并查集
- Summer Training day6 codeforces787D 最短路spfa,线段树建图
- Summer Training day6 codeforces292D并查集合并
- Codeforces Round #353 (Div. 2) D. Tree Construction (二叉搜索树+set)
- Codeforces Round #353 (Div. 2) D. Tree Construction (构造二叉搜索树)
- NBUT 2012 Summer Training - 1 / [D] Dress, Left Dress!
- hust NENU Summer Training 2014 #2 D - Polyomino Decomposer
- D - Minimum palindrome----(2015 summer training #9)
- D - Cut the Cake----(2015 summer training #11)
- LightOJ 1043 Triangle Partitioning(数学公式)
- 多线程实现方案
- Hibernate中的迫切左外连接和左外连接
- 文章标题
- NY oj 36 最长公共子序列
- Summer Training day6 codeforces 675D 二叉搜索树
- webpack进阶——DllPlugin优化打包性能(基于vue-cli)
- ubuntu16.04登录后黑屏再次回到登录页面
- P1134 阶乘问题
- 关于Wireshark按ip地址分类抓包数据
- 多线程之NSThread的使用
- wpf 命令
- 时间日期类
- 源码编译ceph