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),使得每个节点的值是原来的节点值加上所有大于它的节点值之和.例如:输入: 二叉搜索树: ...
随机推荐
- [Mac][转] ports命令
[Mac][转] ports命令 安装路径:/opt/local/lib/ 常用命令 port -d selfupdate #升级macport, 如同:cd /usr/ports && ...
- SSMS查询快捷方式设置
打开SQL Server Mangement Studio,然后依次打开:工具->选项->环境->键盘->查询快捷方式,大家可以看到,SSMS已经自带了12个快捷键,其中3个已 ...
- docker容器中安装vi
容器中输入vi提示 root@e36f8029c9f2:/# vi bash: vi: command not found 解决办法: 1.通过命令获取最新的软件包 apt-get update ap ...
- django media配置
当我们需要向服务器发送图片或视频,需要对这些媒体文件进行保存时,需要指定保存在哪并将保存的路径添加到路由中. 1.设置settings.py MEDIA_URL = '/media/' MEDIA_R ...
- 使用InstallUtil安装或卸载服务
使用InstallUtil安装或卸载服务 一.安装服务: C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe D:\MyServ ...
- IE 直接打印 页面的方式
1. 通过在IE中加载adobe pdf reader 插件 进行直接打印和打印的配置(推荐) <body> <object id="pdf1" width= ...
- 洛谷P4337 [ZJOI2018]线图(状压+搜索+乱搞)
题面 传送门 题解 妈呀调了我整整一天-- 题解太长了不写了可以去看\(shadowice\)巨巨的 //minamoto #include<bits/stdc++.h> #define ...
- scrapy下载中间件,UA池和代理池
一.下载中间件 框架图: 下载中间件(Downloader Middlewares) 位于scrapy引擎和下载器之间的一层组件. - 作用: (1)引擎将请求传递给下载器过程中, 下载中间件可以对请 ...
- jquery源码解析:代码结构分析
本系列是针对jquery2.0.3版本进行的讲解.此版本不支持IE8及以下版本. (function(){ (21, 94) 定义了一些变量和函数, jQuery = function() ...
- NSUserdefault读书笔记
作用 用来存储首选项的.本来首选项是存在磁盘上的,NSUserdefault相当于提供了一个缓存,不用每次都写文件.也就是说设置首选项以后,可以马上读出来,不必先写到磁盘中去. 定期调用synchro ...