编程介的小学生 2017-05-02 04:22 采纳率: 20.5%
浏览 852
已采纳

Cartesian Tree

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

Input consists of multiple test cases.
For each case, the first line contains an integer 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

For each test case, print in the first line 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.
If there are several possible trees, output any one.
Sample Input

7
5 4
2 2
3 9
0 5
1 3
6 6
4 11
Possible Output for Sample Input

YES
2 3 6
0 5 1
1 0 7
5 0 0
2 4 0
1 0 0
3 0 0

  • 写回答

2条回答 默认 最新

  • threenewbee 2017-05-02 16:08
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog