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 ...
随机推荐
- struct TABLE
struct TABLE { TABLE() {} /* Remove gcc warning */ TABLE_SHARE *s; handler *file; TABLE *next, *prev ...
- 终极解决方案:windows10开机黑屏,死机
windows10开机黑屏,死机一般情况都是由于双显卡中的独立显卡驱动造成的! 那么试着升级一下你的BIOS吧!一定要在官网下载你对应的BIOS驱动,然后双击安装,这个时候就别再动机子了,让他自己刷新 ...
- UVa 10892 (GCD) LCM Cardinality
我一直相信这道题有十分巧妙的解法的,去搜了好多题解发现有的太过玄妙不能领会. 最简单的就是枚举n的所有约数,然后二重循环找lcm(a, b) = n的个数 #include <cstdio> ...
- bzoj2466: [中山市选2009]树
同上一题.(应该可以树形dp,然而我不会... #include<cstdio> #include<cstring> #include<iostream> #inc ...
- Jqgrid入门-操作表格的数据(二)
上一篇中,Jqgrid已经可以从服务端获得数据,并显示在Grid表格中了.下面说一下,如何操作表格及其数据. jqGrid有很多方法函数,用来操作数据或者操作Grid表格本身.jq ...
- highcharts 柱状图动态设置数据应用实例
<div id="container" style="min-width:700px;height:400px"></div> #jav ...
- rtp h264注意点(FU-A分包方式说明)
前写过一篇文章,分析了h264使用rtp进行封包的格式介绍:RTP封装h264.但里面好像没有把拆分以及一些需要注意的情况说清楚,因此这里做补充,也作为自己的备忘(自己记性好像不太好). 关于时间戳, ...
- lnmp脚本
#!/bin/sh echo "欢迎使用 lnmp 脚本 (fanshengshuai@gmail.com) "; echo "增加资源..."; rpm -i ...
- 【转】 IOS中定时器NSTimer的开启与关闭
原文网址:http://blog.csdn.net/enuola/article/details/8099461 调用一次计时器方法: myTimer = [NSTimer scheduledTime ...
- 【转】linux中wait与waitpid的差别
原文网址:http://blog.163.com/libo_5/blog/static/15696852010324287748/ zombie不占用内存也不占用CPU,表面上我们可以不用在乎它们的存 ...