1557_CartesianTree

2022-5-16 18:17| 发布者: Hocassian| 查看: 38| 评论: 0|原作者: 肇庆学院ACM合集

摘要:
C:\Users\Administrator\Downloads\2019-10-12-10-14-2-89503967390700-Problem List-采集的数据-后羿采集器.html

Pro.ID

1557

Title

Cartesian Tree

Title链接

http://10.20.2.8/oj/exercise/problem?problem_id=1557

AC

0

Submit

0

Ratio

-

时间&空间限制

  • Time Limit: 10000/5000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others)
  • 描述

    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 yL(x) then ky < kx

    • if zL(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.

    输入

    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 ≤ 50000 ). The following N lines contain two numbers each --- given pairs (ki, ai). For each pair |ki|, |ai| ≤ 30000. All main keys and all auxiliary keys are different, i.e. kikj and aiaj for each ij.

    输出

    Description

    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 yL(x) then ky < kx

    • if zL(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 ≤ 50000 ). The following N lines contain two numbers each --- given pairs (ki, ai). For each pair |ki|, |ai| ≤ 30000. All main keys and all auxiliary keys are different, i.e. kikj and aiaj for each ij.

    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

    Source

    样例输入

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

    样例输出

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

    作者


    路过

    雷人

    握手

    鲜花

    鸡蛋

    最新评论

    返回顶部