POJ-2201 Cartesian Tree(笛卡尔树)
来源:互联网 发布:d3.js百度百科 编辑:程序博客网 时间:2024/05/26 09:53
笛卡尔树是一棵二叉树,树的每个节点有两个值,一个为key,一个为value。光看key的话,笛卡尔树是一棵二叉搜索树,每个节点的左子树的key都比它小,右子树都比它大;光看value的话,笛卡尔树有点类似堆,根节点的value是最小(或者最大)的,每个节点的value都比它的子树要大。
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <vector>#include <stack>#include <queue> #define maxn 50005#define INF 1e9typedef long long ll;using namespace std;struct Node{ int l1, r1, pre, p, l2, r2; int k, v;}node[maxn];int n, Stack[maxn];void Build(){ Stack[1] = 1; int t = 1; for(int i = 2; i <= n; i++){ while(t > 0 && node[Stack[t]].v > node[i].v) t--; if(t){ int d = Stack[t]; node[i].l1 = node[d].r1; node[i].pre = node[d].p; node[d].r1 = node[i].p; node[i].l2 = node[d].r2; node[d].r2 = i; node[node[i].l2].pre = node[i].p; Stack[++t] = i; } else{ node[i].l1 = node[Stack[1]].p; node[Stack[1]].pre = node[i].p; node[i].l2 = Stack[1]; Stack[++t] = i; } }}int cmp(const Node&a, const Node&b){ return a.k < b.k;}int temp(const Node&a, const Node&b){ return a.p < b.p;}int main(){// freopen("in.txt", "r", stdin); while(scanf("%d", &n) == 1){ for(int i = 1; i <= n; i++){ scanf("%d%d", &node[i].k, &node[i].v); node[i].l1 = node[i].r1 = node[i].pre = 0; node[i].p = i; } sort(node+1, node+1+n, cmp); Build(); sort(node+1, node+1+n, temp); puts("YES"); for(int i = 1; i <= n; i++){ printf("%d %d %d\n", node[i].pre, node[i].l1, node[i].r1); } } return 0;}
0 0
- POJ-2201 Cartesian Tree【笛卡尔树】
- POJ 2201 Cartesian Tree 笛卡尔树
- POJ 2201 Cartesian Tree 【笛卡尔树】
- POJ 2201 Cartesian Tree (笛卡尔树)
- POJ-2201-Cartesian Tree(笛卡尔树)
- POJ 2201 Cartesian Tree 笛卡尔树
- POJ-2201 Cartesian Tree(笛卡尔树)
- poj 2201 Cartesian Tree 笛卡尔树
- POJ 2201 Cartesian Tree 笛卡尔树裸题加讲解
- 笛卡尔树cartesian tree
- 笛卡尔树(Cartesian Tree)
- SGU 155 Cartesian Tree(笛卡尔树)
- sgu 155 Cartesian Tree (poj2201) 笛卡尔树构造
- poj 北大oj 2201 Cartesian Tree
- poj 2201 笛卡尔树
- poj 2201 笛卡尔树
- poj 2201 笛卡尔树
- RDD笛卡尔操作Cartesian
- NOIP2008P4 立体图 题解
- c++计算最大公约数
- 作为Java程序员这些都不知道那就真的OUT了!
- vmware下虚拟机centos 桥接上网
- 重建二叉树
- POJ-2201 Cartesian Tree(笛卡尔树)
- 如何加快C++代码的编译速度
- IEEE802.11数据帧在Linux上的抓取
- XPath与lxml_4功能函数
- 最长公共子序列问题 LCS
- Android VelocityTracker 速度跟踪器
- Codeforces Round #356 (Div. 2) [Codeforces680]
- JAR/WAR/EAR区别
- phpMyAdmin自动生成密码忘记怎么办?