【代码】

 #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的更多相关文章

  1. Algorithm: cartesian tree

    http://baike.baidu.com/link?url=XUt5fXQ-jtFBM0UdKiGA41_NWFvdFSYwVsy4SVvCRRuEBvNkLfT9TgOtzsXvaOT9nuq_ ...

  2. [sgu P155] Cartesian Tree

    155. Cartesian Tree time limit per test: 0.25 sec. memory limit per test: 65536 KB input: standard i ...

  3. 笛卡尔树Cartesian Tree

    前言 最近做题目,已经不止一次用到笛卡尔树了.这种数据结构极为优秀,但是构造的细节很容易出错.因此写一篇文章做一个总结. 笛卡尔树 Cartesian Tree 引入问题 有N条的长条状的矩形,宽度都 ...

  4. 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 ...

  5. 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 ...

  6. PAT-1167(Cartesian Tree)根据中序遍历序列重建最小堆

    Cartesian Tree PAT-1167 一开始我使用数组进行存储,但是这样可能会导致无法开足够大的数组,因为树如果是链表状的则无法开这么大的数组(虽然结点很少). 正确的解法还是需要建树,使用 ...

  7. POJ 2201 Cartesian Tree ——笛卡尔树

    [题目分析] 构造一颗笛卡尔树,然后输出这棵树即可. 首先进行排序,然后用一个栈维护最右的树的节点信息,插入的时候按照第二关键字去找,找到之后插入,下面的树成为它的左子树即可. 然后插入分三种情况讨论 ...

  8. SGU 155.Cartesian Tree

    时间限制:0.25s 空间限制:6M 题意: 给出n(n< 50000)个含双关键字(key,val)的节点,构造一颗树使该树,按key值是一颗二分查找树,按val值是一个小根堆. Soluti ...

  9. CF1290E Cartesian Tree

    考虑笛卡尔树的意义: 一个点在笛卡尔树中的子树,代表以他为最小/最大值的区间. 所以一个点的子树大小,一定是类似到达序列边界或者被一个比他更大的数隔离. 考虑记录 \(l_i,r_i\) 为第 \(i ...

随机推荐

  1. 使用ipns 为ipfs 系统自定义域名

    ipns 可以帮助我们进行寻址操作,但是默认的hashid 还是太长,不好记忆,ipns 同时也支持 基于域名的解析,我们添加txt 记录就可以方便的解决ipfs 文件访问地址难记的问题,使用的是 一 ...

  2. loki grafana 团队开源的,类似Prometheus 的log 系统

    Prometheus 主要面向的是metrics,但是loki 是log,这样加上grafana 强大的可视化以及alert能力, 我们可以做好多事情,loki 的设计来源于Prometheus. 组 ...

  3. Web 前端面试小知识

    简历投递 前期为了解自身短板, 可以海投一些试试. 不建议长期海投简历, 对用人单位简历筛选和你自身都没什么好处. 投简历之前最起码要关注以下几点(薪资范围, 公司位置, 职位要求, 是否为培训机构冒 ...

  4. java-jdbc数据库连接

    web.xml:(web.xml) <!-- 加载spring容器 --> <context-param> <param-name>contextConfigLoc ...

  5. Redis list数据结构

    1. 更换命名空间 2.lpush 往列表里放入10个值 3. llen列表长度 4. lrange 列表范围 8. lset将第0个元素设置为88 9. lindex 获得第2个元素 10. lpo ...

  6. CentOS7局域网下安装离线Ambari

    1 Ambari介绍.安装与应用案例介绍 1.1 Ambari Ambari 跟 Hadoop 等开源软件一样,也是 Apache Software Foundation 中的一个项目,并且是顶级项目 ...

  7. .net 读取/保存 文件 到 局域网 服务器

    public class IdentityScope : IDisposable { /// <summary> /// 登录一个新用户 /// </summary> /// ...

  8. kali 安装qq

    使用的是longene TM2013 下载地址(百度云):http://pan.baidu.com/s/1dFx8azv 安装: 64位的需要安装32位依赖文件 用这条命令 apt-get insta ...

  9. 配置 influxDB 鉴权及 HTTP API 写数据的方法

    本文简要描述如何为 InfluxDB 开启鉴权和配置用户管理权限(安装后默认不需要登录),以及开启鉴权后如何使用 HTTP API 写数据. 创建 InfluxDB 管理员账号创建 admin 帐号密 ...

  10. C#实现根据日期计算星期

    /// <summary> /// 根据日期返回 星期(返回结果为英文) /// </summary> /// <param name="date"& ...