Codeforces Round #353 (Div. 2) D. Tree Construction (二分,stl_set)
题目链接: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)的更多相关文章
- Codeforces Round #353 (Div. 2) D. Tree Construction 模拟
D. Tree Construction 题目连接: http://www.codeforces.com/contest/675/problem/D Description During the pr ...
- 数据结构 - Codeforces Round #353 (Div. 2) D. Tree Construction
Tree Construction Problem's Link ------------------------------------------------------------------- ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树
题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- Codeforces Round #353 (Div. 2)
数学 A - Infinite Sequence 等差数列,公差是0的时候特判 #include <bits/stdc++.h> typedef long long ll; const i ...
- 线段树+dp+贪心 Codeforces Round #353 (Div. 2) E
http://codeforces.com/contest/675/problem/E 题目大意:有n个车站,每个车站只能买一张票,这张票能从i+1到a[i].定义p[i][j]为从i到j所需要买的最 ...
- 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 ...
- 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 ...
随机推荐
- Java关键字static、final使用小结
static 1. static变量 按照是否静态的对类成员变量进行分类可分两种:一种是被static修饰的变量,叫静态变量或类变量:另一种是没有被static修饰的变量,叫实例变量.两者的 ...
- HDU 4906 状态压缩dp
Our happy ending Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- bzoj1564
嗯,这是一道简单题 注意二叉搜索树的子树中序一定是连续的 又因为取值修改是任意的并且修改代价与权值无关 不难想到把权值离散化,然后按找数据值排序,然后dp f[i][j][w]表示从i~j的节点构成一 ...
- bzoj3632
裸的最大团,随机化大法好 多次随机出一个选择顺序然后贪心即可 ..,..] of boolean; a:..] of longint; v:..] of boolean; n,m,i,j,x,y,an ...
- ActivityManager: Warning: Activity not started, its current task has been brought to the front 的的问题
运行android程序的时候提示:ActivityManager: Warning: Activity not started, its current task has been brought t ...
- jQuery实战读书笔记(备忘录)
选择器备忘: | :even 匹配所有索引值为偶数的元素,从 0 开始计数 :odd 匹配所有索引值为奇数的元素,从 0 开始计数 实例——设置table交替行变色: <script type= ...
- 多线程程序设计学习(2)之single threaded execution pattern
Single Threaded Execution Pattern[独木桥模式] 一:single threaded execution pattern的参与者--->SharedResourc ...
- 通过userAgent判断手机浏览器类型
我们可以通过userAgent来判断,比如检测某些关键字,例如:AppleWebKit*****Mobile或AppleWebKit,需要注意的是有些浏览器的userAgent中并不包含AppleWe ...
- [Everyday Mathematics]20150203
设 $f$ 在 $\bbR$ 上连续可导, 且 $\dps{f'\sex{\frac{1}{2}}=0}$. 试证: $$\bex \exists\ \xi\in \sex{0,\frac{1}{2} ...
- Android ListView从网络获取图片及文字显示
上一篇文章说的是ListView展示本地的图片以及文本,这一篇说一下如何从网络获取图片以及文本来显示.事实上,一般是先获取Josn或sml数据,然后解释显示.我们先从网上获取xml,然后对其进行解析, ...