• Day6


    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+10;
    
    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 = 1; i <= N; ++i) {
            scanf("%d%d", &Nodes[i].key, &Nodes[i].value);
            Nodes[i].id = i;
        }
        sort(Nodes+1, Nodes+N+1);
        int top = 0;
        bool flag = false;
        for(int i = 1; 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
    ");
        for(int i = 1; i <= N; ++i)
            printf("%d %d %d
    ", fa[i], Left[i], Right[i]);
        return 0;
    }
    View Code
  • 相关阅读:
    postman批量运行和参数化
    Loadrunner 参数化数据分配方法以及数据更新方式
    常用函数
    获取 layer.msg 弹窗的信息
    登录页脚本小结
    关于linux下system()函数的总结
    请不要重复犯我在学习Python和Linux系统上的错误
    教你摸清 Linux PC 的性能底细?
    Ubuntu GNOME 16.10 Beta 1问世了!
    Google疯了,竟然这样!
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/12205539.html
Copyright © 2020-2023  润新知