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
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
Source
Northeastern Europe 2002, Northern Subregion
题目类型:笛卡尔树的构建
算法分析:直接使用O(n)的右链算法构建出一个笛卡尔树,最后进行一次遍历将所有节点的儿子父亲关系找到,然后直接输出。如果所给节点中存在相同的key值的话,直接输出NO,这是因为BST中是不可能出现相同key的。在向笛卡尔树中插入节点时要注意修改待插入节点的左孩子的父节点!!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
#pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define LL long long #define ULL unsigned long long #define DB(ccc) cout << #ccc << " = " << ccc << endl const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 5e4 + 66; struct node { int l, r, par, key; int v, rnd; }; bool operator < (const node &aa, const node &bb) {return aa.v < bb.v;} node dt[maxn], res[maxn]; bool vis[maxn]; void Init () { for (int i = 0; i < maxn; i++) { dt[i].l = dt[i].r = dt[i].par = 0; dt[i].rnd = -INF; } } void Insert (int rt) { int i = rt - 1; while (dt[i].rnd > dt[rt].rnd) i = dt[i].par; dt[rt].l = dt[i].r; dt[dt[i].r].par = rt; dt[rt].par = i; dt[i].r = rt; } int main() { // CFF; int n; while (scanf ("%d", &n) != EOF) { memset (vis, false, sizeof (vis)); bool is_valid = true; Init (); for (int i = 1; i <= n; i++) { scanf ("%d%d", &dt[i].v, &dt[i].rnd); dt[i].key = i; if (vis[dt[i].v]) is_valid = false; else vis[dt[i].v] = true; } if (!is_valid) puts ("NO"); else { puts ("YES"); sort (dt + 1, dt + 1 + n); for (int i = 1; i <= n; i++) Insert (i); for (int i = 1; i <= n; i++) { if (!dt[i].l) res[dt[i].key].l = 0; else res[dt[i].key].l = dt[dt[i].l].key; if (!dt[i].r) res[dt[i].key].r = 0; else res[dt[i].key].r = dt[dt[i].r].key; if (!dt[i].par) res[dt[i].key].par = 0; else res[dt[i].key].par = dt[dt[i].par].key; } for (int i = 1; i <= n; i++) printf ("%d %d %d\n", res[i].par, res[i].l, res[i].r); } } return 0; } |
- « 上一篇:poj2142
- poj2234:下一篇 »