Let us consider a special type of a binary search tree, called a cartesian tree. Recall that a binary search tree is a rooted ordered binary tree, such that for its every node x the following condition is satisfied: each node in its left subtree has the key less then the key of x, and each node in its right subtree has the key greater then the key of x.
That is, if we denote left subtree of the node x by L(x), its right subtree by R(x) and its key by kx then for each node x we have

  • if y ∈ L(x) then ky < kx
  • if z ∈ R(x) then kz > kx

The binary search tree is called cartesian if its every node x in addition to the main key kx also has an auxiliary key that we will denote by ax, and for these keys the heap condition is satisfied, that is

  • if y is the parent of x then ay < ax

Thus a cartesian tree is a binary rooted ordered tree, such that each of its nodes has a pair of two keys (k, a) and three conditions described are satisfied.
Given a set of pairs, construct a cartesian tree out of them, or detect that it is not possible.

Input

The first line of the input file contains an integer number N -- the number of pairs you should build cartesian tree out of (1 <= N <= 50 000). The following N lines contain two numbers each -- given pairs (ki, ai). For each pair |ki|, |ai| <= 30 000. All main keys and all auxiliary keys are different, i.e. ki != kj and ai != aj for each i != j.

Output

On the first line of the output file print YES if it is possible to build a cartesian tree out of given pairs or NO if it is not. If the answer is positive, on the following N lines output the tree. Let nodes be numbered from 1 to N corresponding to pairs they contain as they are given in the input file. For each node output three numbers -- its parent, its left child and its right child. If the node has no parent or no corresponding child, output 0 instead.
The input ensure these is only one possible tree.

Sample Input

7
5 4
2 2
3 9
0 5
1 3
6 6
4 11

Sample Output

YES
2 3 6
0 5 1
1 0 7
5 0 0
2 4 0
1 0 0
3 0 0 思路:裸的笛卡尔树,学习新知识,这题输入唯一,一定有解,参考博客:https://blog.csdn.net/qq_36056315/article/details/79845193
https://blog.csdn.net/code92007/article/details/94591571
注意不要在退栈的时候改变fa指针就行,要根据退栈完毕后left和right指针进行fa的更改,不然已经定好的顺序会乱(
const int maxm = 5e4+;

int fa[maxm], Left[maxm], Right[maxm], N;

struct Node {
int key, value, id;
bool operator<(const Node &node) const {
return key < node.key;
}
} Nodes[maxm], s[maxm]; int main() {
scanf("%d", &N);
for(int i = ; i <= N; ++i) {
scanf("%d%d", &Nodes[i].key, &Nodes[i].value);
Nodes[i].id = i;
}
sort(Nodes+, Nodes+N+);
int top = ;
bool flag = false;
for(int i = ; i <= N; ++i) {
while(top && s[top].value > Nodes[i].value) {
Left[Nodes[i].id] = s[top].id;
top--;
}
fa[Nodes[i].id] = s[top].id;
fa[Left[Nodes[i].id]] = Nodes[i].id;
if(top)
Right[s[top].id] = Nodes[i].id;
s[++top] = Nodes[i]; }
printf("YES\n");
for(int i = ; i <= N; ++i)
printf("%d %d %d\n", fa[i], Left[i], Right[i]);
return ;
}

Day6 - J - Cartesian Tree POJ - 2201的更多相关文章

  1. [sgu P155] Cartesian Tree

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

  2. Apple Tree POJ - 2486

    Apple Tree POJ - 2486 题目大意:一棵点带权有根树,根节点为1.从根节点出发,走k步,求能收集的最大权值和. 树形dp.复杂度可能是O(玄学),不会超过$O(nk^2)$.(反正这 ...

  3. E - Apple Tree POJ - 2486

    E - Apple Tree POJ - 2486 Wshxzt is a lovely girl. She likes apple very much. One day HX takes her t ...

  4. Algorithm: cartesian tree

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

  5. 笛卡尔树Cartesian Tree

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

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

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

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

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

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

  9. 第十四届华中科技大学程序设计竞赛 J Various Tree【数值型一维BFS/最小步数】

    链接:https://www.nowcoder.com/acm/contest/106/J 来源:牛客网 题目描述 It's universally acknowledged that there'r ...

随机推荐

  1. ES5-严格模式

    在es5中可以开启一种严格模式的代码形式,开启方式是:将全局或者函数的第一条语句定义为:'use strict';. 如果浏览器不支持,会将其解析为一条普通语句,没有任何的副作用. 开启全局模式后会有 ...

  2. http的3次握手与4次挥手

    Http的3次握手: 第一次握手:客户端发送一个带SYN的TCP报文到服务器,表示客户端想要和服务器端建立连接. 第二次握手:服务器端接收到客户端的请求,返回客户端报文,这个报文带有SYN和ACK确认 ...

  3. [Qt5] 使用Qt设计器绘制主窗口

    实践代码: git clone https://github.com/dilexliu/learn_qt5.git Step1: Qt设计器绘制窗口 保存会得到一个文件: mainwindow.ui ...

  4. 记录—JPA生成数据库表

    环境 springBoot+JPA+MySQL application-dev.yml 注意:配置中的blog数据库需要先创建,否则启动springBoot会报错 spring: #数据库连接配置 d ...

  5. python表白代码1.0桃心输出

    python爱心表达函数初级版本:def my_heart(a,b=2): print("\n".join(["".join([(a[(x-y) % len(a ...

  6. 为Linux环境安装图形化界面

    1.更新软件源并升级系统 yum uppdate&&yum install wqy-microhei-fonts 2.安装GNOME+VNC一键包 wget https://gist. ...

  7. 微信web版接口api(转)

    安卓微信的api,个人微信开发API协议,微信 ipad sdk,微信ipad协议,微信web版接口api,微信网页版接口,微信电脑版sdk,微信开发sdk,微信开发API,微信协议,微信接口文档sd ...

  8. windows环境变量引发的血案

    最近重装了系统,决心使用Anaconda来管理python包和虚拟环境.在完成一系列配置后,运行程序,发现老是报错 D:\Anaconda3\envs\jobnote>python E:\wor ...

  9. Python 关于super在多继承中的解析

    一.单继承情况: 解析: 创建B类的实例化对象时,执行初始化函数: 打印输出Enter B,当遇到super()调用父类初始化函数(此时是调用B类的父类A的__init__函数),输出Enter A. ...

  10. JS原生编写实现留言板功能

    实现这个留言板功能比较简单,所以先上效果图: 实现用户留言内容,留言具体时间. <script> window.onload = function(){ var oMessageBox = ...