Day6 - J - Cartesian Tree POJ - 2201
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
Output
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 思路:裸的笛卡尔树,学习新知识,这题输入唯一,一定有解,参考博客:https://blog.csdn.net/qq_36056315/article/details/79845193
https://blog.csdn.net/code92007/article/details/94591571
注意不要在退栈的时候改变fa指针就行,要根据退栈完毕后left和right指针进行fa的更改,不然已经定好的顺序会乱(
const int maxm = 5e4+; int fa[maxm], Left[maxm], Right[maxm], N; struct Node {
int key, value, id;
bool operator<(const Node &node) const {
return key < node.key;
}
} Nodes[maxm], s[maxm]; int main() {
scanf("%d", &N);
for(int i = ; i <= N; ++i) {
scanf("%d%d", &Nodes[i].key, &Nodes[i].value);
Nodes[i].id = i;
}
sort(Nodes+, Nodes+N+);
int top = ;
bool flag = false;
for(int i = ; i <= N; ++i) {
while(top && s[top].value > Nodes[i].value) {
Left[Nodes[i].id] = s[top].id;
top--;
}
fa[Nodes[i].id] = s[top].id;
fa[Left[Nodes[i].id]] = Nodes[i].id;
if(top)
Right[s[top].id] = Nodes[i].id;
s[++top] = Nodes[i]; }
printf("YES\n");
for(int i = ; i <= N; ++i)
printf("%d %d %d\n", fa[i], Left[i], Right[i]);
return ;
}
Day6 - J - Cartesian Tree POJ - 2201的更多相关文章
- [sgu P155] Cartesian Tree
155. Cartesian Tree time limit per test: 0.25 sec. memory limit per test: 65536 KB input: standard i ...
- Apple Tree POJ - 2486
Apple Tree POJ - 2486 题目大意:一棵点带权有根树,根节点为1.从根节点出发,走k步,求能收集的最大权值和. 树形dp.复杂度可能是O(玄学),不会超过$O(nk^2)$.(反正这 ...
- E - Apple Tree POJ - 2486
E - Apple Tree POJ - 2486 Wshxzt is a lovely girl. She likes apple very much. One day HX takes her t ...
- Algorithm: cartesian tree
http://baike.baidu.com/link?url=XUt5fXQ-jtFBM0UdKiGA41_NWFvdFSYwVsy4SVvCRRuEBvNkLfT9TgOtzsXvaOT9nuq_ ...
- 笛卡尔树Cartesian Tree
前言 最近做题目,已经不止一次用到笛卡尔树了.这种数据结构极为优秀,但是构造的细节很容易出错.因此写一篇文章做一个总结. 笛卡尔树 Cartesian Tree 引入问题 有N条的长条状的矩形,宽度都 ...
- PAT-2019年冬季考试-甲级 7-4 Cartesian Tree (30分)(最小堆的中序遍历求层序遍历,递归建树bfs层序)
7-4 Cartesian Tree (30分) A Cartesian tree is a binary tree constructed from a sequence of distinct ...
- PAT-1167(Cartesian Tree)根据中序遍历序列重建最小堆
Cartesian Tree PAT-1167 一开始我使用数组进行存储,但是这样可能会导致无法开足够大的数组,因为树如果是链表状的则无法开这么大的数组(虽然结点很少). 正确的解法还是需要建树,使用 ...
- POJ 2201 Cartesian Tree ——笛卡尔树
[题目分析] 构造一颗笛卡尔树,然后输出这棵树即可. 首先进行排序,然后用一个栈维护最右的树的节点信息,插入的时候按照第二关键字去找,找到之后插入,下面的树成为它的左子树即可. 然后插入分三种情况讨论 ...
- 第十四届华中科技大学程序设计竞赛 J Various Tree【数值型一维BFS/最小步数】
链接:https://www.nowcoder.com/acm/contest/106/J 来源:牛客网 题目描述 It's universally acknowledged that there'r ...
随机推荐
- 文件目录T位
场景: 共享目录设置T标志 可以看别人的文件,但不可以删除.修改别人的文件 除非是ROOT,目录的拥有者
- C#多态学习总结
面向对象编程三大特点 封装 继承 多态.今天我把自己学习多态的过程进行总结 多态 就是 同一个方法在不同情况下,会表选不同的效果(多个形态).在代码上表现就是 同一个父类对象 赋予不同的子类对象 就 ...
- Linux centosVMware 告警系统主脚本、告警系统配置文件、告警系统负载脚本、告警系统502日志脚本、告警系统disk监控脚本、告警系统邮件引擎
一.告警系统主脚本 要求:我们的机器角色多种多样,但是所有机器上都要部署同样的监控系统,也就说所有机器不管什么角色,整个程序框架都是一致的,不同的地方在于根据不同的角色,定制不同的配置文件. 程序架构 ...
- Python 基础之推导式
一.列表推导式 通过一行循环判断,遍历出一系列数据的方式就是推导式 特点:方便,简洁,可以实现一些简单的功能推导式当中只能跟循环和判断(单项分支)种类分为三种: 列表推导式 集合推导式 字典推导式 ...
- DoMes平台首页菜单栏
问题1:左侧菜单栏数据是在哪里获取的? 答案1: 项目根目录的Views/Home/Index文件为平台首页 打开Index.cshtml文件,有一个framework-clientdata.js引入 ...
- 使用CSS3动画属性实现各种旋转跳跃
Transform字面上就是变形,改变的意思.在CSS3中transform主要包括以下几种:旋转rotate.扭曲skew.缩放scale和移动translate以及矩阵变形matrix. tran ...
- 关于Redis 分布式 微服务 集群Cluster
一:Redis 1,redis是一个高性能的键值对存储方式的数据库,同时还提供list,set,zset,hash等数据结构的存储. 2,Redis运行在内存中但是可以持久化到磁盘,所以在对不同数据集 ...
- C#用SQLDMO操作数据库----转载
C#用SQLDMO操作数据库 sqldmo.dll是随sql server2000一起发布的.sqldmo.dll自身是一个com对象 sqldmo(sql distributed managemen ...
- C# Stream篇(—) -- Stream基类
写在前头: Stream系列文章共收录7篇,本着备忘和归纳的目的本着备忘和归纳的目的,全部收录于本分类中. 下面是有原文连接,望各位看官还是到原作者处学习,毕竟CV过来的文字难免有走样之处. 原始连接 ...
- 数据库事务ACID特效
一.数据库事务正确执行的4个基础要素: 1.原子性 整个事务中的所有操作,要么全部完成,要么全部不完成,不可能停滞在中间某个环节.事务在执行过程中发生错误,会被回滚(Rollback)到事务开始前的状 ...