题目链接:http://codeforces.com/problemset/problem/675/D

给你一个如题的二叉树,让你求出每个节点的父节点是多少。

用set来存储每个数,遍历到a[i]的时候查找比a[i]大的数的位置,然后插入,而父亲就是刚好比a[i]小的数或刚好大的数。

然后讨论是哪一个数。

比如给你3 1 2 ,如图

1的父亲是3 ,2的父亲是1。

那我其实只要找左边或右边出现最晚的数就行了,用pair的first表示a[i],second表示出现的顺序i。

 #include <bits/stdc++.h>
using namespace std;
typedef pair <int , int> P;
int a[int(1e5 + )] , ans[int(1e5 + )];
set <P> v;
int main()
{
int n;
scanf("%d" , &n);
for(int i = ; i < n ; ++i) {
if(i == ) {
scanf("%d" , a + i);
v.insert(P(a[i] , i));
continue;
}
scanf("%d" , a + i);
auto it = v.upper_bound(P(a[i] , ));
if(it == v.begin()) {
ans[i] = it->first;
}
else if(it == v.end()) {
ans[i] = (--it)->first;
}
else {
if(it->second > (--it)->second)
ans[i] = (++it)->first;
else
ans[i] = it->first;
}
v.insert(P(a[i] , i));
}
for(int i = ; i < n - ; ++i)
printf("%d " , ans[i]);
printf("%d\n" , ans[n - ]);
}

Codeforces Round #353 (Div. 2) D. Tree Construction (二分,stl_set)的更多相关文章

  1. Codeforces Round #353 (Div. 2) D. Tree Construction 模拟

    D. Tree Construction 题目连接: http://www.codeforces.com/contest/675/problem/D Description During the pr ...

  2. 数据结构 - Codeforces Round #353 (Div. 2) D. Tree Construction

    Tree Construction Problem's Link ------------------------------------------------------------------- ...

  3. Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树

    题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...

  4. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  5. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  6. Codeforces Round #353 (Div. 2)

    数学 A - Infinite Sequence 等差数列,公差是0的时候特判 #include <bits/stdc++.h> typedef long long ll; const i ...

  7. 线段树+dp+贪心 Codeforces Round #353 (Div. 2) E

    http://codeforces.com/contest/675/problem/E 题目大意:有n个车站,每个车站只能买一张票,这张票能从i+1到a[i].定义p[i][j]为从i到j所需要买的最 ...

  8. Codeforces Round #540 (Div. 3)--1118F1 - Tree Cutting (Easy Version)

    https://codeforces.com/contest/1118/problem/F1 #include<bits/stdc++.h> using namespace std; in ...

  9. Codeforces Round #353 (Div. 2) E. Trains and Statistic dp 贪心

    E. Trains and Statistic 题目连接: http://www.codeforces.com/contest/675/problem/E Description Vasya comm ...

随机推荐

  1. 最大流 Dinic + Sap 模板

    不说别的,直接上模板. Dinic+当前弧优化: struct Edge{ int x,y,c,ne; }e[M*]; int be[N],all; int d[N],q[N]; int stack[ ...

  2. c#里面的namespace基础(一)

    我现在感到学好C#就是就是要知道,C#的基本语法,C#的新的特点,C#能干什么! 其中我感到不管如何,NAMESPACE都是很关键的,可以说不是只对C#而言,而是整个.NET都是由NAMESPACE组 ...

  3. LA 3890 (半平面交) Most Distant Point from the Sea

    题意: 给出一个凸n边形,求多边形内部一点使得该点到边的最小距离最大. 分析: 最小值最大可以用二分. 多边形每条边的左边是一个半平面,将这n个半平面向左移动距离x,则将这个凸多边形缩小了.如果这n个 ...

  4. Java 知识点:序列化

    首先明确一点:默认的序列化方法速度很慢,因为需要对整个对象和他的类都进行保存,因此我们建议自定义序列化格式. ObjectInputStream和ObjectOutputStream 用途 Objec ...

  5. iOS 静态类库项目的建立与使用

    iOS 静态类库项目的建立与使用   新建 Xcode workspace 打开 Xcode , 选择 File -> New -> Workspace , 将 Workspace 命名为 ...

  6. HelloX操作系统与中国移动OneNET物联网平台成功完成对接

    HelloX成功与中国移动物联网平台对接 经过HelloX项目组同仁的努力,尤其是Tywin(@飓风)的努力下,HelloX最新版本V1.78已成功与中国移动OneNET(open.iot.10086 ...

  7. Android 如何使用juv-rtmp-client.jar向Red5服务器发布实时视频数据

    使用juv-client-client.jar主要是尽快地完成毕业设计里面手机端向网页端发送实时视频的功能,由于实习和做毕业设计的时间冲突,因此完成毕业设计只花了1个多月时间. (万恶的形式主义,论文 ...

  8. js如何判断是否在iframe中及防止网页被别站用 iframe嵌套 (Load denied by X-Frame-Options)

    1. js如何判断是否在iframe中 //方式一 if (self.frameElement && self.frameElement.tagName == "IFRAME ...

  9. 图片的android:src 及android:background共存

    ---恢复内容开始--- 需求:给ImageView添加背景色 效果: 实现分析: 1.目录结构: 代码实现: 1.activity_main.xml <merge xmlns:android= ...

  10. 在英文 sql2005中 比较nvarchar 与 varchar的速度

    declare @str1 varchar(max); declare @count int; ; print 'begin' begin set @str1 = @str1 + '*'; ; end ...