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 ...
随机推荐
- poj-1782 Run Length Encoding
http://poj.org/problem?id=1782 Run Length Encoding Time Limit: 1000MS Memory Limit: 30000K Total S ...
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- eclipse编辑器,怎么创建PHP和JAVA的工程项目?
eclipse编辑器,怎么创建PHP和JAVA的工程项目.强大的eclipse,编写php或者java由你选择!!! 我为什么这么说? 我是新手,写下这篇文章,可能对新手会有一点点的帮助而已,然后为了 ...
- WinForm中关于控件焦点的问题
方法一: 在打开一个窗体时,我们往往需要设置焦点让光标出现在我们希望它出现的位置上. 这时我们可以在窗体的Activated事件中设置焦点 例如我们希望光标在打开窗体的时候出现在textBox1上,我 ...
- poj 3294 Life Forms(后缀数组)
题意:给你最多100个字符串,求最长的且是一半以上的字符串的公共子串,如果有多个,按字典序输出. 思路:先把各个串拼起来,中间加上一个之前未出现过的字符,然后求后缀.然后根据h数组和sa数组,求出最长 ...
- UVA - 10785 The Mad Numerologist
题目链接 这个题又犯了省题不清的错误.导致不停 wa.唉. 题目意思是给你一个长度L,然后和一张表相应每一个大写字母的value值.你须要依照一定规则找出长度为L的序列. 注意 序列的value值要 ...
- 史上最简单的带流控功能的http server
s.py import time import SimpleHTTPServer import SocketServer BYTES_PER_SECOND=160*1024 class MyHTTPR ...
- git 工具
https://www.kernel.org/pub/software/scm/git/ wget https://www.kernel.org/pub/software/scm/git/git-2. ...
- Android Activity横竖屏转换的生命周期
新创建一个Activity,用来此次测试. 先贴代码 package com.hugo.apj.activitylifetest; import android.support.v7.app.AppC ...
- STL的基本使用之关联容器:map和multiMap的基本使用
STL的基本使用之关联容器:map和multiMap的基本使用 简介 map 和 multimap 内部也都是使用红黑树来实现,他们存储的是键值对,并且会自动将元素的key进行排序.两者不同在于map ...