SGU 155.Cartesian Tree
时间限制:0.25s
空间限制:6M
题意:
给出n(n< 50000)个含双关键字(key,val)的节点,构造一颗树使该树,按key值是一颗二分查找树,按val值是一个小根堆.
Solution :
先按k值从小到大排序.
再从序列中找到最小的val值,将其作为根.再对它的左边和右边做同样的操作.左边最大的数做左儿子,右边做右儿子。递归即可.
这里要快速找到一个序列区间的最大值,用ST方法求RMQ就行了.
时间复杂度O(nlogN),空间复杂度O(n)
code:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
using namespace std; struct node {
int key, val, ID;
} p; struct answer {
int fa, lson, rson;
} ans[51000]; typedef pair<int , int > P;
vector<node> f;
P st[51000][20];
int n, x, y; bool cmp (node a, node b) {
return a.key < b.key;
}
//ST RMQ
void ST() {
for (int i = n - 1; i >= 0 ; i--)
for (int j = 1; i + (1 << j) <= n; j++)
{
if (st[i][j - 1].first < st[i + (1 << j - 1)][j - 1].first)
st[i][j] = st[i][j - 1];
else
st[i][j] = st[i + (1 << j - 1)][j - 1];
}
}
//得到区间最小值的位置
int getmin (int l, int r)
{
P tem;
tem=st[l][0];
for (int k = 0; l + (1 << k) <= r; k++)
{
if (st[l][k].first < tem.first)
tem = st[l][k];
if (st[r - (1 << k) + 1][k].first < tem.first)
tem = st[r - (1 << k) + 1][k];
}
return tem.second;
}
//递归建树
int make (int l, int r, int fa)
{
int k = getmin (l, r);
int pos = f[k].ID;
ans[pos].fa = fa;
if (l >= r) return pos;
if (l < k) ans[pos].lson = make (l, k - 1, pos);
if (k < r) ans[pos].rson = make (k + 1, r, pos);
return pos;
} int main()
{
scanf("%d",&n);
for (int i = 0; i < n; i++)
{
scanf("%d %d",&x,&y);
p.key = x, p.val = y, p.ID = i + 1;
f.push_back (p);
}
//按key值从小到大排序
sort (f.begin(), f.end(), cmp);
for (int i = 0; i < (int) f.size(); i++)
st[i][0] = make_pair (f[i].val, i);
ST();
make (0, n - 1, 0);
//一定有解直接输出 "YES"
puts("YES\n");
for (int i = 1; i <= n; i++)
printf("%d %d %d\n",ans[i].fa,ans[i].lson,ans[i].rson);
return 0;
}
SGU 155.Cartesian Tree的更多相关文章
- [sgu P155] Cartesian Tree
155. Cartesian Tree time limit per test: 0.25 sec. memory limit per test: 65536 KB input: standard i ...
- 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 ...
- Day6 - J - Cartesian Tree POJ - 2201
Let us consider a special type of a binary search tree, called a cartesian tree. Recall that a binar ...
- PAT-1167(Cartesian Tree)根据中序遍历序列重建最小堆
Cartesian Tree PAT-1167 一开始我使用数组进行存储,但是这样可能会导致无法开足够大的数组,因为树如果是链表状的则无法开这么大的数组(虽然结点很少). 正确的解法还是需要建树,使用 ...
- POJ 2201 Cartesian Tree ——笛卡尔树
[题目分析] 构造一颗笛卡尔树,然后输出这棵树即可. 首先进行排序,然后用一个栈维护最右的树的节点信息,插入的时候按照第二关键字去找,找到之后插入,下面的树成为它的左子树即可. 然后插入分三种情况讨论 ...
- OpenJudge Cartesian Tree
[代码] #include <cstdio> #include <cstdlib> #include <cstring> #include <algorith ...
- CF1290E Cartesian Tree
考虑笛卡尔树的意义: 一个点在笛卡尔树中的子树,代表以他为最小/最大值的区间. 所以一个点的子树大小,一定是类似到达序列边界或者被一个比他更大的数隔离. 考虑记录 \(l_i,r_i\) 为第 \(i ...
随机推荐
- 余弦距离、欧氏距离和杰卡德相似性度量的对比分析 by ChaoSimple
1.余弦距离 余弦距离,也称为余弦相似度,是用向量空间中两个向量夹角的余弦值作为衡量两个个体间差异的大小的度量. 向量,是多维空间中有方向的线段,如果两个向量的方向一致,即夹角接近零,那么这两个向 ...
- 【转】Ubuntu14.04搭建安装svnserver
原文网址:http://www.cnblogs.com/blfshiye/p/5168028.html 前两天,公司准备搭建一个svnserver,供大家使用.于是.就先装了一个Ubuntu系统,然后 ...
- eclipse配置j2ee项目
1.下载jdk (1.5,1.6) 安装 从sun的官方网站下载,我下的是jdk-1_5_0_19-nb-6_5_1-windows-ml.exe,集成netbean的版本,下载后一路默认安装. 配置 ...
- MIPI D-PHY 简写收集
Acronyms APPI Abstracted PHY-Protocol InterfaceBER Bit Error Rate 417 CILControl ...
- [git] git 分支( branch ) 的基本使用
分支( branches ) 是指在开发主线中分离出来,做进一步开发而不影响到原来主线. Git 存储的不是一系列的更改集( changeset ),而是一系列快照.当你执行一次 commit 时, ...
- Android NDK开发指南---Application.mk文件和android.mk文件
https://android.googlesource.com/platform/development/+/donut-release/ndk/docs/OVERVIEW.TXT https:// ...
- Java 内存泄露的理解与解决过程
本文详细地介绍了Java内存管理的原理,以及内存泄露产生的原因,同时提供了一些列解决Java内存泄露的方案,希望对各位Java开发者有所帮助. Java内存管理机制 在C++ 语言中,如果需要动态分配 ...
- 深入浅出node(4) 异步编程
一)函数式编程基础 二)异步编程的优势和难点 2.1 优势 2.2 难点 2.2.1 异常处理 2.2.2 函数嵌套过深 2.2.3 阻塞 2.2.4 多线程编程 2.2.5 异步转同步 三)异步编程 ...
- bootstrap+jQuery.validate表单校验
谈谈表单校验 这大概是一种惯例,学习前台后台最开始接触的业务都是用户注册和登录.现在社会坚持以人为本的理念,在网站开发过程同样如此.User是我们面对较多的对象,也是较核心的对象.最开始的用户注册和登 ...
- 如何解决 SogouIinput not enough space for thread data ?? 虚拟内存
问题:总是提示没有足够的空间读写数据 上图: 原因: 可能是虚拟内存设置了过大了[我不知道明白是不是也是这样,我出现这个问题就是因为我把虚拟内存设置成了4G,我的物理内存是2G的] 具体问题具体分析, ...