OpenJudge Cartesian Tree



【代码】
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = ;
//Tkey为输入主键与辅键的结构体
//key表示主键,aux表示辅键,index表示是输入的第几个结点
struct Tkey {
int key, aux, index;
} keys[maxn];
//Tnode是BST结点的结构体,key表示主键,aux表示辅键
//father表示父结点的编号,leftChild和rightChild表示左右儿子结点
struct Tnode {
int key, aux, father, leftChild, rightChild;
} node[maxn];
int n; //排序的比较函数
bool cmp(const Tkey &a, const Tkey &b) {
return a.key < b.key;
} int main() {
//读入数据
int i;
scanf("%d", &n);
for (i = ; i <= n; ++i) {
scanf("%d%d", &keys[i].key, &keys[i].aux);
keys[i].index = i;
} //按key对结点排序
sort(keys + , keys + n + , cmp); //按key从小到大将结点插入BST
//father表示当前插入结点的父节点,leftChild表示当前插入结点的左儿子节点
//rightMost表示每次插入前BST最右的结点
int p, father, leftChild, rightMost = ;
for (i = ; i <= n; ++i) {
//寻找插入结点的父亲与左儿子
leftChild = ; father = rightMost;
while (father != && node[father].aux > keys[i].aux) {
leftChild = father;
father = node[father].father;
}
//将结点插入BST
p = keys[i].index;
node[p].key = keys[i].key;
node[p].aux = keys[i].aux;
node[p].father = father;
node[p].leftChild = leftChild;
node[p].rightChild = ;
if (father != )
node[father].rightChild = p;
if (leftChild != )
node[leftChild].father = p;
rightMost = keys[i].index;
} //输出答案
printf("YES\n");
for (i = ; i <= n; ++i)
printf("%d %d %d\n", node[i].father, node[i].leftChild, node[i].rightChild);
return ;
}
OpenJudge Cartesian Tree的更多相关文章
- Algorithm: cartesian tree
http://baike.baidu.com/link?url=XUt5fXQ-jtFBM0UdKiGA41_NWFvdFSYwVsy4SVvCRRuEBvNkLfT9TgOtzsXvaOT9nuq_ ...
- [sgu P155] Cartesian Tree
155. Cartesian Tree time limit per test: 0.25 sec. memory limit per test: 65536 KB input: standard i ...
- 笛卡尔树Cartesian Tree
前言 最近做题目,已经不止一次用到笛卡尔树了.这种数据结构极为优秀,但是构造的细节很容易出错.因此写一篇文章做一个总结. 笛卡尔树 Cartesian Tree 引入问题 有N条的长条状的矩形,宽度都 ...
- PAT-2019年冬季考试-甲级 7-4 Cartesian Tree (30分)(最小堆的中序遍历求层序遍历,递归建树bfs层序)
7-4 Cartesian Tree (30分) A Cartesian tree is a binary tree constructed from a sequence of distinct ...
- Day6 - J - Cartesian Tree POJ - 2201
Let us consider a special type of a binary search tree, called a cartesian tree. Recall that a binar ...
- PAT-1167(Cartesian Tree)根据中序遍历序列重建最小堆
Cartesian Tree PAT-1167 一开始我使用数组进行存储,但是这样可能会导致无法开足够大的数组,因为树如果是链表状的则无法开这么大的数组(虽然结点很少). 正确的解法还是需要建树,使用 ...
- POJ 2201 Cartesian Tree ——笛卡尔树
[题目分析] 构造一颗笛卡尔树,然后输出这棵树即可. 首先进行排序,然后用一个栈维护最右的树的节点信息,插入的时候按照第二关键字去找,找到之后插入,下面的树成为它的左子树即可. 然后插入分三种情况讨论 ...
- SGU 155.Cartesian Tree
时间限制:0.25s 空间限制:6M 题意: 给出n(n< 50000)个含双关键字(key,val)的节点,构造一颗树使该树,按key值是一颗二分查找树,按val值是一个小根堆. Soluti ...
- CF1290E Cartesian Tree
考虑笛卡尔树的意义: 一个点在笛卡尔树中的子树,代表以他为最小/最大值的区间. 所以一个点的子树大小,一定是类似到达序列边界或者被一个比他更大的数隔离. 考虑记录 \(l_i,r_i\) 为第 \(i ...
随机推荐
- MySQL Cursor Demo
-- 使用cursor的demo -- ==============================## -- 删除存储过程 DROP PROCEDURE USP_TestCursor; DELIMI ...
- docusaurus 生成的website 通过circleci部署gh-pages
docusaurus 是facebook 开源的一款文档脚手架工具,可以快速的进行文档生成,基于markdown 同时已经内置了gh-pages 发布的命令,对于ci 工具,我们只需要简单的配置就可以 ...
- Cocos2d-x3.0 TestCPP文件夹笔记
1.不多说,重力加速度. 2.ActionMangerTest:此Test是为了展示通过导演类来获得动作管理器ActionManager类.来控制节点动作. ①CrashTest:销毁demo,在精灵 ...
- S老师 背包系统 装备系统 锻造系统 学习
Inventory using UnityEngine; using System.Collections; using System.Collections.Generic; using Syste ...
- lua C++ wrapper
背景 最近在研究lua的c++绑定库,使用过一下几个 luabind 问题:没人维护 https://github.com/vinniefalco/LuaBridge https://github.c ...
- Reactor/Proactor的比较 (ZZ)
一般情况下,I/O 复用机制需要事件分享器(event demultiplexor [1.3]). 事件分享器的作用,即将那些读写事件源分发给各读写事件的处理者,就像送快递的在楼下喊: 谁的什么东西送 ...
- 用react编写一个hello world
我要分享的是用react搭建一个简单的hello world, 一个小demo, 大神请略过 首先看一下目录结构 创建一个目录, 用于存放demo mkdir reactHello cd reactH ...
- 新版vue-cli输入本地ip不能访问,只能用localhost才可以访问?
问:新版vue-cli输入本地ip不能访问,只能用localhost才可以访问? 答:修改config/index.js配置,将host: 'localhost',改为host: '0.0.0.0', ...
- 学习大数据基础框架hadoop需要什么基础
什么是大数据?进入本世纪以来,尤其是2010年之后,随着互联网特别是移动互联网的发展,数据的增长呈爆炸趋势,已经很难估计全世界的电子设备中存储的数据到底有多少,描述数据系统的数据量的计量单位从MB(1 ...
- Spring Cloud(Dalston.SR5)--Feign 与 Hystrix 断路器整合
创建项目 要使 Feign 与 Hystrix 进行整合,我们需要增加 Feign 和 Hystrix 的依赖,修改 POM.xml 中增加以下依赖项如下: <?xmlversion=" ...