POJ2201 Cartesian Tree (cartesian tree)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
#define ON_DEBUG
#ifdef ON_DEBUG
#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#else
#define D_e_Line ;
#endif
struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std;
const int N = 50007;
struct node{
int l,r,fa,id;
int val,pri;
bool operator < (const node &com)const{
return val < com.val;
}
}t[N],ans[N];
inline void Insert(int rt){
int tmp;
for(tmp = rt - 1; t[tmp].pri > t[rt].pri; tmp = t[tmp].fa);
t[rt].l = t[tmp].r;
t[t[tmp].r].fa = rt;
t[rt].fa = tmp;
t[tmp].r = rt;
}
int vis[30007];
int main(){
int n;
while(scanf("%d", &n) != EOF){
Fill(vis, false);
int flag = true;
R(i,0,n){
t[i].l = t[i].r = t[i].fa = 0;
t[i].w = -0x7fffffff;
}
R(i,1,n){
scanf("%d%d", &t[i].val, &t[i].w);
t[i].id = i;
if(vis[t[i].val])
flag = false;
else
vis[t[i].val] = true;
}
if(!flag){
printf("NO\n");
continue;
}
printf("YES\n");
sort(t + 1, t + n + 1);
R(i,1,n) Insert(i);
R(i,1,n){
if(!t[i].l)
ans[t[i].id].l = 0;
else
ans[t[i].id].l = t[t[i].l].id;
if(!t[i].r)
ans[t[i].id].r = 0;
else
ans[t[i].id].r = t[t[i].r].id;
if(!t[i].fa)
ans[t[i].id].fa = 0;
else
ans[t[i].id].fa = t[t[i].fa].id;
}
R(i,1,n)
printf("%d %d %d\n", ans[i].fa, ans[i].l, ans[i].r);
}
return 0;
}
POJ2201 Cartesian Tree (cartesian tree)的更多相关文章
- ZOJ 3201 Tree of Tree
树形DP.... Tree of Tree Time Limit: 1 Second Memory Limit: 32768 KB You're given a tree with weig ...
- LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]
Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...
- B-tree/B+tree/B*tree [转]
(原文出处:http://blog.csdn.net/hbhhww/article/details/8206846) B~树 1.前言: 动态查找树主要有:二叉查找树(Binary Search Tr ...
- leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree
leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...
- [BZOJ3080]Minimum Variance Spanning Tree/[BZOJ3754]Tree之最小方差树
[BZOJ3080]Minimum Variance Spanning Tree/[BZOJ3754]Tree之最小方差树 题目大意: 给定一个\(n(n\le50)\)个点,\(m(m\le1000 ...
- easyui tree扩展tree方法获取目标节点的一级子节点
Easyui tree扩展tree方法获取目标节点的一级子节点 /* 只返回目标节点的第一级子节点,具体的用法和getChildren方法是一样的 */ $.extend($.fn.tree.meth ...
- Tree - Decision Tree with sklearn source code
After talking about Information theory, now let's come to one of its application - Decision Tree! No ...
- Binary Indexed Tree (Fenwick Tree)
Binary Indexed Tree 主要是为了存储数组前缀或或后缀和,以便计算任意一段的和.其优势在于可以常数时间处理更新(如果不需要更新直接用一个数组存储所有前缀/后缀和即可).空间复杂度O(n ...
- POJ1741 Tree + BZOJ1468 Tree 【点分治】
POJ1741 Tree + BZOJ1468 Tree Description Give a tree with n vertices,each edge has a length(positive ...
- B-tree & B+tree & B*Tree 结构浅析——转
转自http://www.cnblogs.com/coder2012/p/3330311.html http://blog.sina.com.cn/s/blog_6776884e0100ohvr.ht ...
随机推荐
- 关于我学git这档子事(4)
------------恢复内容开始------------ 当本地分支(main/dev)比远程仓库分支(main/dev)落后几次提交时 先: git pull 更新本地仓库 再 git push ...
- vs 快速定位文件
在进行web开发时,我们经常需要在文件之间进行切换,每次在VS的解决方案中找文件然后打开 非常浪费时间,有没有比较快捷点的方法呢? 1.使用 ReSharper 插件 ReSharper 插件可以在 ...
- SpringCloud Alibaba Sentinel 限流详解
点赞再看,养成习惯,微信搜索[牧小农]关注我获取更多资讯,风里雨里,小农等你,很高兴能够成为你的朋友. 项目源码地址:公众号回复 sentinel,即可免费获取源码 熔断规则 在上一篇文章中我们讲解了 ...
- redis+lua实现脚本一键查询
场景 经常需要查redis某个key的值,需要执行三条命令才能查到 redis-cli,启动redis select num,选择db get key,查询语句 需要执行三条命令才能实现某个key的查 ...
- Dubbo的基本使用
Dubbo分为提供者和消费方 并且两者都要注册到ZK上 提供者 注解 @Service 这是dubbo包下的 消费组 注解 @Reference 远程注入 第一步导入依赖 <! ...
- 技术分享 | app自动化测试(Android)--元素定位方式与隐式等待
原文链接 元素定位是 UI 自动化测试中最关键的一步,假如没有定位到元素,也就无法完成对页面的操作.那么在页面中如何定位到想要的元素,本小节讨论 Appium 元素定位方式. Appium的元素定位方 ...
- Java学习-第一部分-第一阶段-第二节:变量
变量 变量介绍 为什么需要变量 变量是程序的基本组成单位 不论是使用哪种高级程序语言编写程序,变量都是其程序的基本组成单位,比如: //变量有三个基本要素(类型+名称+值) class Test{ p ...
- Spring框架系列(9) - Spring AOP实现原理详解之AOP切面的实现
前文,我们分析了Spring IOC的初始化过程和Bean的生命周期等,而Spring AOP也是基于IOC的Bean加载来实现的.本文主要介绍Spring AOP原理解析的切面实现过程(将切面类的所 ...
- 机器学习-K近邻(KNN)算法详解
一.KNN算法描述 KNN(K Near Neighbor):找到k个最近的邻居,即每个样本都可以用它最接近的这k个邻居中所占数量最多的类别来代表.KNN算法属于有监督学习方式的分类算法,所谓K近 ...
- NC15975 小C的记事本
NC15975 小C的记事本 题目 题目描述 小C最近学会了java小程序的开发,他很开心,于是想做一个简单的记事本程序练练手. 他希望他的记事本包含以下功能: 1.append(str),向记事本插 ...