codeforces675D Tree Construction
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。
本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!
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.
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.
正解:set维护平衡树
解题报告:
以前做过的题,只需维护前驱后继即可。
ps:特判两边相等的情况。
//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <complex>
using namespace std;
typedef long long LL;
const int inf = (1<<30);
const int MAXN = 200011;
int n,deep[MAXN],father[MAXN],ql,qr,nowl,nowr,val[MAXN];
set<int>bst;
map<int,int>mp; inline int getint(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
} inline void work(){
n=getint(); int x; bst.insert(inf); bst.insert(-inf);
val[1]=x=getint(); bst.insert(x); mp[x]=1;
for(int i=2;i<=n;i++) {
x=getint(); val[i]=x;
ql=*--bst.lower_bound(x);
qr=*bst.lower_bound(x);
if(ql==(-inf)) father[i]=mp[qr];
else if(qr==inf) father[i]=mp[ql];
else{
if(nowl<nowr) father[i]=mp[ql];
else if(nowl>nowr) father[i]=mp[qr];
else{
int ll=mp[ql],rr=mp[qr];
if(ll>rr) father[i]=ll;
else father[i]=rr;
}
}
deep[i]=deep[father[i]]+1;
bst.insert(x); mp[x]=i;
//printf("%d %d\n",father[i],deep[i]);
printf("%d ",val[father[i]]);
}
} int main()
{
work();
return 0;
}
codeforces675D Tree Construction的更多相关文章
- 数据结构 - Codeforces Round #353 (Div. 2) D. Tree Construction
Tree Construction Problem's Link ------------------------------------------------------------------- ...
- codeforces 675D D. Tree Construction(线段树+BTS)
题目链接: D. Tree Construction D. Tree Construction time limit per test 2 seconds memory limit per test ...
- HDOJ 3516 Tree Construction
四边形优化DP Tree Construction Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 模拟
D. Tree Construction 题目连接: http://www.codeforces.com/contest/675/problem/D Description During the pr ...
- CF 675D——Tree Construction——————【二叉搜索树、STL】
D. Tree Construction time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- STL---Codeforces675D Tree Construction(二叉树节点的父亲节点)
Description During the programming classes Vasya was assigned a difficult problem. However, he doesn ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树
题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...
- Codeforces 675D Tree Construction Splay伸展树
链接:https://codeforces.com/problemset/problem/675/D 题意: 给一个二叉搜索树,一开始为空,不断插入数字,每次插入之后,询问他的父亲节点的权值 题解: ...
- hdu3516 Tree Construction
Problem Description Consider a two-dimensional space with a set of points (xi, yi) that satisfy xi & ...
随机推荐
- HDU4771(2013 Asia Hangzhou Regional Contest )
http://acm.hdu.edu.cn/showproblem.php?pid=4771 题目大意: 给你一幅图(N*M)“@”是起点,"#"是墙,“.”是路,然后图上有K个珠 ...
- 对宽度的控制原则 git commit -a -m "M 1、完成less计算得出图片的均分布局;";git push origin master:master
<script> import wepy from 'wepy' import api from '../api/api' export default class recharge ex ...
- 问题:Unable to find a 'userdata.img' file for ABI armeabi to copy into the AVD folder.
创建AVD时,发现创建不成功,报错“Unable to find a 'userdata.img' file for ABIarmeabi to copy into the AVD folder.” ...
- 如何使用 Opencv dnn 模块调用 Caffe 预训练模型?
QString modelPrototxt = "D:\\Qt\\qmake\\CaffeModelTest\\caffe\\lenet.prototxt"; QString mo ...
- es6数组的一些函数方法使用
数组函数forEach().map().filter().find().every().some().reduce()等 数组函数(这里的回调函数中的index和arr都可以省略,回调函数后有参数是设 ...
- 设置mysql外网访问
任意主机以用户root和密码mypwd连接到mysql服务器mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'myp ...
- 查看hive的配置信息
在启动hive时设置配置属性信息 $ bin/hive --hiveconf <property=value> 查看当前所有的配置信息 hive > set ; hive (db_h ...
- 4.6 基于STM32+MC20地图显示路径功能
需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...
- NodeJS 加入windows7服务 开机运行 nssm
from:http://blog.sina.com.cn/s/blog_5ef5f2630101aql8.html 首先需要到http://nssm.cc/download/?page=downloa ...
- iOS UIWindow 与 windowLevel 学习
Pop几个关键点 KeyWindow :”The key window is the one that is designated to receive keyboard and other non- ...