本文主要是介绍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 heightif(u.height==v.height) return u.name<v.name; //If equal,ascending by namereturn u.height<v.height;
}int down(Person u,Person v) { //descending by heightif(u.height==v.height) return u.name>v.name; //If equal,descending by namereturn u.height>v.height;
}
在主函数中的调用方法如下:
sort(p,p+n,up); //Sort the structured array p by ascending field heightsort(p,p+n,down); //Sort the structured array p by descending field height
【算法代码】
注意: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 11out:
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/
这篇关于POJ 2201:Cartesian Tree ← 笛卡尔树模板题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!