1. 程式人生 > >18.12.20 DSA Cartesian Tree

18.12.20 DSA 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.

程式碼填空

}

輸入

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.輸出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.

樣例輸入

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

提示

#include < cstdio >
#include < cstdlib >
#include < cstring >
#include < algorithm >
using namespace std;

const int maxn = 50005;
//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 = 1; i <= n; ++i) {
        scanf("%d%d", &keys[i].key, &keys[i].aux);
        keys[i].index = i;
    }

    //按key對結點排序
    sort(keys + 1, keys + n + 1, cmp);

    //按key從小到大將結點插入BST
    //father表示當前插入結點的父節點,leftChild表示當前插入結點的左兒子節點
    //rightMost表示每次插入前BST最右的結點
    int p, father, leftChild, rightMost = 0;
    for (i = 1; i <= n; ++i) {
        //尋找插入結點的父親與左兒子
        leftChild = 0; father = rightMost;
        while (father != 0 && _____(1)_____) {
            leftChild = father;
            _____(2)_____
        }
        //將結點插入BST
        p = keys[i].index;
        node[p].key = keys[i].key;
        node[p].aux = keys[i].aux;
        node[p].father = father;
        node[p].leftChild = _____(3)_____;
        node[p].rightChild = _____(4)_____;
        if (father != 0)
            node[father].rightChild = p;
        if (leftChild != 0)
            node[leftChild].father = p;
        _____(5)_____
    }

    //輸出答案
    printf("YES\n");
    for (i = 1; i <= n; ++i)
        printf("%d %d %d\n", node[i].father, node[i].leftChild, node[i].rightChild);
    return 0;
}

題解:

 1 #include <iostream>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <stack>
 5 #include <string>
 6 #include <math.h>
 7 #include <queue>
 8 #include <stdio.h>
 9 #include <string.h>
10 #include <set>
11 #include <vector>
12 #include <fstream>
13 //#define maxn 200005
14 #define inf 999999
15 #define cha 127
16 #define eps 1e-6 
17 #define oo 1503
18 using namespace std;
19 
20 const int maxn = 50005;
21 //Tkey為輸入主鍵與輔鍵的結構體
22 //key表示主鍵,aux表示輔鍵,index表示是輸入的第幾個結點
23 struct Tkey {
24     int key, aux, index;
25 } keys[maxn];
26 //Tnode是BST結點的結構體,key表示主鍵,aux表示輔鍵
27 //father表示父結點的編號,leftChild和rightChild表示左右兒子結點
28 struct Tnode {
29     int key, aux, father, leftChild, rightChild;
30 } node[maxn];
31 int n;
32 
33 //排序的比較函式
34 bool cmp(const Tkey &a, const Tkey &b) {
35     return a.key < b.key;
36 }
37 
38 int main() {
39     //讀入資料
40     int i;
41     scanf("%d", &n);
42     for (i = 1; i <= n; ++i) {
43         scanf("%d%d", &keys[i].key, &keys[i].aux);
44         keys[i].index = i;
45     }
46 
47     //按key對結點排序
48     sort(keys + 1, keys + n + 1, cmp);
49 
50     //按key從小到大將結點插入BST
51     //father表示當前插入結點的父節點,leftChild表示當前插入結點的左兒子節點
52     //rightMost表示每次插入前BST最右的結點
53     int p, father, leftChild, rightMost = 0;
54     for (i = 1; i <= n; ++i) {
55         //尋找插入結點的父親與左兒子
56         leftChild = 0; father = rightMost;
57         while (father != 0 && node[father].aux>=keys[i].aux) {
58             leftChild = father;
59             father = node[father].father;
60         }
61         //將結點插入BST
62         p = keys[i].index;
63         node[p].key = keys[i].key;
64         node[p].aux = keys[i].aux;
65         node[p].father = father;
66         node[p].leftChild = leftChild;
67         node[p].rightChild = 0;
68         if (father != 0)
69             node[father].rightChild = p;
70         if (leftChild != 0)
71             node[leftChild].father = p;
72         rightMost = keys[i].index;
73     }
74 
75     //輸出答案
76     printf("YES\n");
77     for (i = 1; i <= n; ++i)
78         printf("%d %d %d\n", node[i].father, node[i].leftChild, node[i].rightChild);
79     return 0;
80 }
View Code