CF 675D——Tree Construction——————【二叉搜索树、STL】
2 seconds
256 megabytes
standard input
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.
- 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.
3
1 2 3
1 2
5
4 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.
题目大意:给你n个不等的数,第一个数字为二叉搜索树的根。然后剩下的n-1个数字依次插入树中,问你各自的父节点是谁。
解题思路:用常规的建树模拟肯定超时。我们知道,如果一个数x要插入树中,如果存在l < x < r,那么我们只需要找到最大的l和最小的r,且他们中之一必是x的父亲。且x要么插在l的右儿子,或者是插到r的左儿子位置。那么只需要再判断一下要插入的位置是否能插,能插即插。同时用map容器模拟树的形态。
收获:知道了set和map是有序的容器。upper_bound(key)返回从容器中找到第一个大于key的记录的迭代器。lower_bound(key)返回从容器中找到第一个大于等于key的记录的迭代器。如果都小于key,那么返回容器的末尾,即container.end()。
#include <iostream>
#include<algorithm>
#include<stdio.h>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long LL;
const int maxn = 1e5+200;
const int mod = 1e9+7;
#define mid (L+R)/2
#define lson rt*2,L,mid
#define rson mid+1,R
set<int>Set;
set<int>::iterator iter;
map<int,int>Lson;
map<int,int>Rson;
int main(){
int n, x;
while(scanf("%d",&n)!=EOF){
scanf("%d",&x);
Set.insert(x);
int ans;
for(int i = 2; i <= n; i++){
scanf("%d",&x);
iter = Set.upper_bound(x);
if(iter!=Set.end() && Lson.count(*iter) == 0){
Lson[*iter] = x;
ans = *iter;
}else{
iter--;
Rson[*iter] = x;
ans = *iter;
}
Set.insert(x);
printf("%d",ans);
if(i == n){
printf("\n");
}else{
printf(" ");
}
}
}
return 0;
}
CF 675D——Tree Construction——————【二叉搜索树、STL】的更多相关文章
- Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树
题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...
- [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树
4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- [LeetCode] Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [LeetCode] Validate Binary Search Tree 验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- LeetCode 235. Lowest Common Ancestor of a Binary Search Tree (二叉搜索树最近的共同祖先)
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [LeetCode] Convert BST to Greater Tree 将二叉搜索树BST转为较大树
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original B ...
- 538 Convert BST to Greater Tree 把二叉搜索树转换为累加树
给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和.例如:输入: 二叉搜索树: ...
随机推荐
- 学习笔记之Struts2—浅析接收参数
最近自己通过视频与相关书籍的学习,对action里面接收参数做一些总结与自己的理解. 0.0.接收参数的(主要)方法 使用Action的属性接收参数 使用DomainModel接收参数 使用Mod ...
- Visual Studio 2008 SP1键盘F10单步调试超慢解决方法
症状: 中断程序调试时,F10或者其它键盘操作都超级慢. 鼠标点击工具栏的按钮速度正常. 解决方法: 网上说的什么删掉所有断点啦,关掉几个窗口啦,重置用户设置啦,关掉某某调试选项啦,关掉防火墙啦,都是 ...
- 设置CameraRollBrowseOptions的宽高
在利用air的CameraRoll调取ios设备的相册时需要定义位置.我们一般这么操作 var crOpts:CameraRollBrowseOptions = new CameraRollBrows ...
- 当在安卓低版本呈现的界面(H5)出现问题的时候,我们怎么解决?
昨天,在医院现场的客服人员,向我们反馈一个问题:说一位用户用他的安卓手机打开我们的app之后,界面是乱掉的:如下图: 向客服询问了具体的设备信息:安卓系统版本号是4.2 下意识觉得是因为css的兼容问 ...
- 2. Python的划分
解释型:当程序运行时,将代码从上至下,一句一句解释成二进制,在执行. 典型:python,php 优点:开发速度快,可以跨平台. 缺点:执行效率慢 编译型:将源码一次性转化成二进制文件,然后在执行. ...
- zTree API中刷新树没效果
想刷新树,但是根据API来的refresh无效 ---------------------------------------------------------------------------- ...
- UITouch笔记
UITouch是什么 表示在在屏幕上触摸事件,包括触摸的位置.大小.力度(3D touch).运动. 在一系列触摸事件中,UITouch都是同一个,但是不要retain某一个UITouch.如果要保存 ...
- http错误状态码
http://www.kaiyuanba.cn/html/1/131/226/4258.htm 状态代码有三位数字组成,第一个数字定义了响应的类别,且有五种可能取值:1xx:指示信息--表示请求已接收 ...
- Mac下显示和隐藏“隐藏文件”
命令行方式:显示/隐藏Mac隐藏文件命令如下(注意其中的空格并且区分大小写): 显示Mac隐藏文件的命令: defaults write com.apple.finder AppleShowAllFi ...
- css样式之标签的查找
css的组成部分:选择器和声明 css的注释: /*这是注释*/ <!DOCTYPE html> <html lang="zh-CN"> <head& ...