POJ 2201:Cartesian Tree ← 笛卡尔树模板题

【题目来源】
http://poj.org/problem?id=2201

【题目描述】
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

【算法分析】
本题是笛卡尔树的模板题。

● 笛卡尔树
(1)笛卡尔树是由一系列不同数字构成的二叉树。
(2)笛卡尔树的结点有两个属性:键值
val、优先级 pri。其中,键值 val 预设,优先级 pri 随机或预设。笛卡尔树各个结点的键值 val 满足的性质(即根结点的键值 val 比它的左右子树中的结点的 val 值都大或都小),各个结点的优先级 pri 满足二叉搜索树(BST)的性质(即根结点的优先级 pri 比它的左子树中的结点的 pri 值都大,比它的右子树中的结点的 pri 值都小)。
(3)笛卡尔树可由数列构造,在
区间最值查询区间 topK 查询(range top k queries)等问题上有广泛应用。
(4)笛卡尔树结点键值
val 的中序遍历序列为构建其的原始序列。最小堆笛卡尔树表示满足小根堆性质的笛卡尔树。

● 利用数组模拟栈:
https://blog.csdn.net/hnjzsyjyj/article/details/130522133

#include <bits/stdc++.h>
using namespace std;
 
const int maxn=105;
char s[maxn];
 
int main() {
    char x;
    int top=-1;
    
    int n;
    cin>>n;
    while(n--) {
        cin>>x;
        s[++top]=x;
    }
 
    while(top!=-1) {
        cout<<s[top];
        top--;
    }
 
    return 0;
}
 
 
/*
in:
5
abcde
out:
edcba
*/

● 按结构体某一字段对结构体数组进行排序
https://blog.csdn.net/hnjzsyjyj/article/details/120184972
按结构体某一字段对结构体数组进行排序,需要自定义函数up()、down()并调用。详细内容如下:
自定义的结构体Person的内容如下:

struct Person {
	string name;
	int age;
	float height;
};

自定义的比较函数up()、down()的内容如下:

int up(Person u,Person v) { //ascending by height
	if(u.height==v.height) return u.name<v.name; //If equal,ascending by name
	return u.height<v.height;
}
 
int down(Person u,Person v) { //descending by height
	if(u.height==v.height) return u.name>v.name; //If equal,descending by name
	return u.height>v.height;
}

在主函数中的调用方法如下:

sort(p,p+n,up); //Sort the structured array p by ascending field height
 
sort(p,p+n,down); //Sort the structured array p by descending field height

【算法代码一】
注意:POJ 不支持万能头文件。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <stack>
using namespace std;

const int maxn=50005;
int stk[maxn],pre[maxn],rson[maxn],lson[maxn];

struct Node {
    int val;
    int pri;
    int id;
} a[maxn];

int up(Node x,Node y) {
    return x.pri<y.pri;
}

stack<Node> ST; //Monotone stack
void buildCartesianTree(int n) {
    for(int i=1; i<=n; i++) {
        Node u=a[i];
        Node t;
        bool flag=0;
        while(!ST.empty() && u.val<ST.top().val) {
            t=ST.top();
            ST.pop();
            flag=1;
        }

        if(flag) {
            pre[t.id]=u.id;
            lson[u.id]=t.id;
        }

        if(!ST.empty()) {
            pre[u.id]=ST.top().id;
            rson[ST.top().id]=u.id;
        } else pre[u.id]=0;

        ST.push(u);
    }
}

int main() {
    int n;
    cin>>n;
    for(int i=1; i<=n; i++) {
        cin>>a[i].pri>>a[i].val;
        a[i].id=i;
    }

    sort(a+1,a+1+n,up);
    buildCartesianTree(n);

    cout<<"YES"<<endl;
    for(int i=1; i<=n; i++) {
        cout<<pre[i]<<" "<<lson[i]<<" "<<rson[i]<<endl;
    }
    return 0;
}

/*
in:
7
5 4
2 2
3 9
0 5
1 3
6 6
4 11

out:
YES
2 3 6
0 5 1
1 0 7
5 0 0
2 4 0
1 0 0
3 0 0
*/


【算法代码二】
注意:POJ 不支持万能头文件。

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

const int maxn=50005;
int stk[maxn],pre[maxn],rson[maxn],lson[maxn];

struct Node {
    int val;
    int pri;
    int id;
} a[maxn];

int up(Node x,Node y) {
    return x.pri<y.pri;
}

void buildCartesianTree(int n) {
    int top=-1;
    int k;
    for(int i=0; i<n; i++) {
        k=top;
        while(k>=0 && a[stk[k]+1].val>a[i+1].val) k--;
        if(k>-1) {
            pre[a[i+1].id]=a[stk[k]+1].id;
            rson[a[stk[k]+1].id]=a[i+1].id;
        }
        if(k<top) {
            pre[a[stk[k+1]+1].id]=a[i+1].id;
            lson[a[i+1].id]=a[stk[k+1]+1].id;
        }
        stk[++k]=i;
        top=k;
    }
    pre[a[stk[0]+1].id]=0;
}

int main() {
    int n;
    cin>>n;
    for(int i=1; i<=n; i++) {
        cin>>a[i].pri>>a[i].val;
        a[i].id=i;
    }

    memset(stk,-1,sizeof(stk));
    sort(a+1,a+n+1,up);
    buildCartesianTree(n);

    cout<<"YES"<<endl;
    for(int i=1; i<=n; i++) {
        cout<<pre[i]<<" "<<lson[i]<<" "<<rson[i]<<endl;
    }

    return 0;
}

/*
in:
7
5 4
2 2
3 9
0 5
1 3
6 6
4 11

out:
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/lms1256012967/article/details/41978589/
https://upimg.baike.so.com/doc/1828710-1933996.html
https://blog.csdn.net/Only_AiR/article/details/52510514
https://codeleading.com/article/90331629198/







 

  • 30
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值