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

2024-04-30 10:20

本文主要是介绍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 ← 笛卡尔树模板题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/948611

相关文章

基于Python实现一个Windows Tree命令工具

《基于Python实现一个WindowsTree命令工具》今天想要在Windows平台的CMD命令终端窗口中使用像Linux下的tree命令,打印一下目录结构层级树,然而还真有tree命令,但是发现... 目录引言实现代码使用说明可用选项示例用法功能特点添加到环境变量方法一:创建批处理文件并添加到PATH1

Java如何根据word模板导出数据

《Java如何根据word模板导出数据》这篇文章主要为大家详细介绍了Java如何实现根据word模板导出数据,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... pom.XML文件导入依赖 <dependency> <groupId>cn.afterturn</groupId>

Python中Flask模板的使用与高级技巧详解

《Python中Flask模板的使用与高级技巧详解》在Web开发中,直接将HTML代码写在Python文件中会导致诸多问题,Flask内置了Jinja2模板引擎,完美解决了这些问题,下面我们就来看看F... 目录一、模板渲染基础1.1 为什么需要模板引擎1.2 第一个模板渲染示例1.3 模板渲染原理二、模板

利用Python打造一个Excel记账模板

《利用Python打造一个Excel记账模板》这篇文章主要为大家详细介绍了如何使用Python打造一个超实用的Excel记账模板,可以帮助大家高效管理财务,迈向财富自由之路,感兴趣的小伙伴快跟随小编一... 目录设置预算百分比超支标红预警记账模板功能介绍基础记账预算管理可视化分析摸鱼时间理财法碎片时间利用财

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

IDEA自动生成注释模板的配置教程

《IDEA自动生成注释模板的配置教程》本文介绍了如何在IntelliJIDEA中配置类和方法的注释模板,包括自动生成项目名称、包名、日期和时间等内容,以及如何定制参数和返回值的注释格式,需要的朋友可以... 目录项目场景配置方法类注释模板定义类开头的注释步骤类注释效果方法注释模板定义方法开头的注释步骤方法注

C++中函数模板与类模板的简单使用及区别介绍

《C++中函数模板与类模板的简单使用及区别介绍》这篇文章介绍了C++中的模板机制,包括函数模板和类模板的概念、语法和实际应用,函数模板通过类型参数实现泛型操作,而类模板允许创建可处理多种数据类型的类,... 目录一、函数模板定义语法真实示例二、类模板三、关键区别四、注意事项 ‌在C++中,模板是实现泛型编程

基于Java实现模板填充Word

《基于Java实现模板填充Word》这篇文章主要为大家详细介绍了如何用Java实现按产品经理提供的Word模板填充数据,并以word或pdf形式导出,有需要的小伙伴可以参考一下... Java实现按模板填充wor编程d本文讲解的需求是:我们需要把数据库中的某些数据按照 产品经理提供的 word模板,把数据

poj3468(线段树成段更新模板题)

题意:包括两个操作:1、将[a.b]上的数字加上v;2、查询区间[a,b]上的和 下面的介绍是下解题思路: 首先介绍  lazy-tag思想:用一个变量记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。 比如现在需要对[a,b]区间值进行加c操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,

C++11第三弹:lambda表达式 | 新的类功能 | 模板的可变参数

🌈个人主页: 南桥几晴秋 🌈C++专栏: 南桥谈C++ 🌈C语言专栏: C语言学习系列 🌈Linux学习专栏: 南桥谈Linux 🌈数据结构学习专栏: 数据结构杂谈 🌈数据库学习专栏: 南桥谈MySQL 🌈Qt学习专栏: 南桥谈Qt 🌈菜鸡代码练习: 练习随想记录 🌈git学习: 南桥谈Git 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈�