sgu 155 Cartesian Tree

来源:互联网 发布:三国杀技巧知乎 编辑:程序博客网 时间:2024/05/06 06:34

155. Cartesian Tree

time limit per test: 0.5 sec.
memory limit per test: 65536 KB
input: standard input
output: standard output



Let us consider a special type of binary search trees, called cartesian trees. Recall that a binary searchtree 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 than the key of x, and each node in its right subtree has the key greater than the key of x.
That is, if we denote the left subtree of the node x by L(x), its right subtree by R(x) and its key by kx, for each node x we will have
  * if y in L(x) then ky < kx
  * if z in 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 <= 50000). The following N lines contain two integer numbers each - given pairs (ki, ai). For each pair |ki|, |ai| <= 30000. 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, output the tree itself in the following N lines. Let the nodes be numbered from 1 to N corresponding to pairs they contain as these pairs 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 test(s)

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

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

就是求一颗笛卡尔树,我是用排第二key的方法,结果TLE#15 ,想了想知道数据有退化的二叉树,后来翻了模板(模板果然是神器啊) 然后就A了,其实按第一key排序,然后每次从右加,调整树,就是一个出入栈的关系。
还是模板好,我是弱菜。

贴模板:
#include<iostream>#include<cstring>#include<cstdio>#include<set>#include<algorithm>#include<vector>#include<cstdlib>#define inf 0xfffffff#define CLR(a,b) memset((a),(b),sizeof((a)))#define FOR(a,b) for(int a=1;a<=(b);(a)++)using namespace std;int const nMax = 51010;int const base = 10;typedef int LL;typedef pair<LL,LL> pij;//    std::ios::sync_with_stdio(false);struct node{    int k,a,id;    bool operator <(const node& b) const {        return k<b.k;    }} p[nMax],st[nMax];int L[nMax],R[nMax],F[nMax];int n;int main(){    scanf("%d",&n);    for(int i=1;i<=n;i++){        scanf("%d%d",&p[i].k,&p[i].a);        p[i].id=i;        L[i]=R[i]=F[i]=0;    }    sort(p+1,p+n+1);    int top=0;    for(int i=1;i<=n;i++){        int k=top-1;        while(k>=0&&p[i].a<st[k].a){            k--;        }        if(k>=0){            R[st[k].id]=p[i].id;            F[p[i].id]=st[k].id;        }        if(k!=top-1){            L[p[i].id]=st[k+1].id;            F[st[k+1].id]=p[i].id;        }        st[k+1]=p[i];        top=k+2;    }    printf("YES\n");    FOR(i,n) printf("%d %d %d\n",F[i],L[i],R[i]);        return 0;}



原创粉丝点击