题意:给你n个数字,第一个点作为根节点,然后每次插入一个节点,构建一棵平衡二叉树,并输出插入节点后该节点的父节点的值

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-;
const int inf =0x7f7f7f7f;
const double pi=acos(-);
const int maxn=; set<int> s;
map<int,int> l;
map<int,int> r; int main()
{
int n,v;
while(~scanf("%d",&n))
{
s.clear();
l.clear();
r.clear(); scanf("%d",&v);
s.insert(v);
for(int i=;i<=n;i++)
{
scanf("%d",&v);
set<int>::iterator it=s.lower_bound(v);
if(it==s.end())
{
it--;
r[*it]=v;
printf("%d ",*it);
s.insert(v);
continue;
} if(l[*it]==)
l[*it]=v;
else{
it--;
r[*it]=v;
}
printf("%d ",*it);
s.insert(v);
}
printf("\n");
}
return ;
}

先前写的爆内存了,,因为最多可以2^1e5,,所以应该改成map,,不过用set模拟BST不太懂啊、、。。链接

  

TTTTTTTTTTTT Codeforces Round #353 (Div. 2) D 平衡二叉树的set模拟 没有很懂的更多相关文章

  1. Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)

    Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...

  2. Codeforces Round #353 (Div. 2) C Money Transfers

    题目链接: http://www.codeforces.com/contest/675/problem/C 题意: 给一个数组,每个数与他相邻的数相连,第一个与最后一个相连,每个数的数值可以左右移动, ...

  3. Codeforces Round #353 (Div. 2) E. Trains and Statistic 线段树+dp

    题目链接: http://www.codeforces.com/contest/675/problem/E 题意: 对于第i个站,它与i+1到a[i]的站有路相连,先在求所有站点i到站点j的最短距离之 ...

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

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

  5. Codeforces Round #353 (Div. 2) C. Money Transfers (思维题)

    题目链接:http://codeforces.com/contest/675/problem/C 给你n个bank,1~n形成一个环,每个bank有一个值,但是保证所有值的和为0.有一个操作是每个相邻 ...

  6. Codeforces Round #353 (Div. 2) D. Tree Construction (二分,stl_set)

    题目链接:http://codeforces.com/problemset/problem/675/D 给你一个如题的二叉树,让你求出每个节点的父节点是多少. 用set来存储每个数,遍历到a[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 #353 (Div. 2) E. Trains and Statistic dp 贪心

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

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

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

随机推荐

  1. java this的作用

    this: 区分成员变量和局部变量的重名问题,this.name指的是类中的成员变量,而不是方法内部的

  2. MySQL_入手<一>增--数据库操作

    创建数据库 create database db_sanguo charset utf8; 切进db_sanguo use db_sanguo 创建英雄 create table t_hero( id ...

  3. 模块之re模块 正则表达式

    正则表达式,正则表达式在处理字符串上有先天的优势,尤其大数量的字符串.先来记一个网站,此网站功能就是关于正则表达式方面的应用http://tool.chinaz.com/regex/ 单纯的正则表达式 ...

  4. leecode100热题 HOT 100

    # 题名 题解 通过率 难度 出现频率     1 两数之和       46.5% 简单     2 两数相加       35.5% 中等     3 无重复字符的最长子串       31.1% ...

  5. IDEA导入Eclipse 非Maven的Web项目

  6. 使用xpath爬取猫眼电影排行榜

    最近在学习xpath,在网上找资料的时候,发现一个新手经常拿来练手的项目,爬取猫眼电影前一百名排行的信息,很多都是跟崔庆才的很雷同,基本照抄.这里就用xpath自己写了一个程序,同样也是爬取猫眼电影, ...

  7. cookie和session的详解和区别

    会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...

  8. vue组件之属性Props

    组件的属性和事件 父子组件之间的通信 父子组件之间的通信就是 props down,events up,父组件通过 属性props向下传递数据给子组件,子组件通过 事件events 给父组件发送消息. ...

  9. exits 和no exits

    exists : 强调的是是否返回结果集,不要求知道返回什么, 比如:  select name from student where sex = 'm' and mark exists(select ...

  10. idea安装完成后要做的几件事(设置字体、编码、行号)

    1.设置字体大小和样式 打开设置:File-->Settings 看到如下界面,输入font,点击Editor目录下的Font设置字体大小和样式: Font:字体样式 size:字体大小 Fal ...