Algorithm: cartesian tree
这里有详细介绍
有一道coding test的题目给你一个int n, 一串float的数,要你实时打印出当前数到这个数前n个数这n个数里最大值,没有n个数就是前面那几个数的最大值。这里就可以用cartesian tree,label记录是数的下标,p表示数值。插入操作为lg(n), 删除操作也为lg(n),总的复杂度为Nlg(n).
#include <iostream>
#include <fstream>
#include <cstdio> using namespace std; class treap_node
{
public:
int label;
float p;
treap_node *left;
treap_node *right;
treap_node()
{
left = NULL;
right = NULL;
}
}; class treap:treap_node
{
public:
treap_node *root;
treap()
{
root = NULL;
}
void treap_left_rotate(treap_node* &a)
{
treap_node *b = a->right;
a->right = b->left;
b->left = a;
a = b;
}
void treap_right_rotate(treap_node* &a)
{
treap_node *b = a->left;
a->left = b->right;
b->right = a;
a = b;
}
void treap_insert(treap_node* &a, int &label, float &p)
{
if (!a)
{
a = new treap_node;
a->label = label;
a->p = p;
}
else if (label > a->label)
{
treap_insert(a->right, label, p);
if (a->right->p > a->p)
treap_left_rotate(a);
}
else
{
treap_insert(a->left, label, p);
if (a->left->p < a->p)
treap_right_rotate(a);
}
}
void treap_delete_smallestP(treap_node* &a)
{
treap_node *p = a;
treap_node *pre = NULL;
while (p->left != NULL)
{
pre = p;
p = p->left;
}
if (pre != NULL)
{
pre->left = p->right;
}
else
a = p->right;
return;
}
void plist(treap_node *a)
{
if (a != NULL)
{
cout << "(";
plist(a->left);
cout << a->label << "/" << a->p;
plist(a->right);
cout << ")";
}
}
}; int atoi(char *s)
{
int ret = ;
while (*s != '\0') {
ret = ret * + (int)(*s - '');
s++;
}
return ret;
} int main(int argc, char **argv)
{
if (argc != ) {
cout << "invalid input" << endl;
return ;
}
//cout << argv[1] << " " << argv[2] << endl;
ifstream fin(argv[]);
if (!fin) {
cout << "unable to open the file" << endl;
return ;
}
int n = atoi(argv[]);
int count = ;
treap *p = new treap;
float s;
while (fin >> s) {
cout << s << " ";
if (count >= n)
{
p->treap_delete_smallestP(p->root);
}
p->treap_insert(p->root, count, s);
p->plist(p->root);
cout << p->root->p << endl;
count++;
}
return ;
}
Algorithm: cartesian tree的更多相关文章
- PAT-1167(Cartesian Tree)根据中序遍历序列重建最小堆
Cartesian Tree PAT-1167 一开始我使用数组进行存储,但是这样可能会导致无法开足够大的数组,因为树如果是链表状的则无法开这么大的数组(虽然结点很少). 正确的解法还是需要建树,使用 ...
- [sgu P155] Cartesian Tree
155. Cartesian Tree time limit per test: 0.25 sec. memory limit per test: 65536 KB input: standard i ...
- 笛卡尔树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 ...
- POJ 2201 Cartesian Tree ——笛卡尔树
[题目分析] 构造一颗笛卡尔树,然后输出这棵树即可. 首先进行排序,然后用一个栈维护最右的树的节点信息,插入的时候按照第二关键字去找,找到之后插入,下面的树成为它的左子树即可. 然后插入分三种情况讨论 ...
- SGU 155.Cartesian Tree
时间限制:0.25s 空间限制:6M 题意: 给出n(n< 50000)个含双关键字(key,val)的节点,构造一颗树使该树,按key值是一颗二分查找树,按val值是一个小根堆. Soluti ...
- OpenJudge Cartesian Tree
[代码] #include <cstdio> #include <cstdlib> #include <cstring> #include <algorith ...
- [Algorithm] Binary tree: Level Order Traversal
function Node(val) { return { val, left: null, right: null }; } function Tree() { return { root: nul ...
随机推荐
- xml基础学习笔记01
注意:刚刚看了网上对于XML中的标签,节点和元素?到底应该怎么表述?起初我也有这个疑惑,现在我的想法是:下面出现node的应称作节点,节点对象.element应称作元素,毕竟这更符合英文的本意.至于标 ...
- Netsharp快速入门(之8) 基础档案(工作区2 设置商品主列表、规格细列表、商品表单、查询)
作者:秋时 杨昶 时间:2014-02-15 转载须说明出处 3.5.1.1 列表设置 1.选择第一行主列表,点工具-列表方案 2.打开列表方案界面后,在列表项目填入需要用到实体Demo.Arc ...
- Unity的物理引擎是如何实现碰撞的呢?
物理引擎不允许两个碰撞器重叠,当两个或多个物体碰撞时,Unity会根 据他们的旋转速度计算碰撞效果.计算主要根据物体的碰撞器是静止的还 是动态的.物体是不移动的,例如,墙,地面,院子里的喷池等.动态物 ...
- SharedPreferences实现记住密码功能
SharedPerferences 简单介绍 用于保存简单的键值对数据: 它将数据放在 /data/data/<package name>/shared_prefs目录下,用xml文件保存 ...
- Google Guava学习笔记——基础工具类String处理类的使用
不管你喜欢何种编程语言,很多时候针对string编程的处理都是乏味而且爱出错误的,很多时候,我们需要从文件或是数据库中读取数据,或者根据需求重新格式化或排序字符串给用户显示.幸运的是,Guava提供了 ...
- 中断(interrupt)、异常(exception)、陷入(trap)
原文出处:http://lhk518.blog.163.com/blog/static/3153998320084263554749/ 中断:是为了设备与CPU之间的通信.典型的有如服务请求,任务完成 ...
- 偏序集的Dilworth定理
定理1 令(X,≤)是一个有限偏序集,并令r是其最大链的大小.则X可以被划分成r个但不能再少的反链.其对偶定理称为Dilworth定理:定理2 令(X,≤)是一个有限偏序集,并令m是反链的最大的大小. ...
- acdream1116 Gao the string!(hash二分 or 后缀数组)
问题套了一个斐波那契数,归根结底就是要求对于所有后缀s[i...n-1],所有前缀在其中出现的总次数.我一开始做的时候想了好久,后来看了别人的解法才恍然大悟.对于一个后缀来说 s[i...n-1]来说 ...
- HDU4782 Beautiful Soup
成都赛里的一道坑爹码力题,突然间脑抽想做一下弥补一下当时的遗憾.当时没做出这道题一是因为当时只剩大概45分钟,对于这样的具有各种条件的题无从下手,二则是因为当时估算着已经有银牌了,所以就不挣扎了.但是 ...
- flume-ng+Kafka+Storm+HDFS 实时系统搭建
转自:http://www.tuicool.com/articles/mMrQnu7 一 直以来都想接触Storm实时计算这块的东西,最近在群里看到上海一哥们罗宝写的Flume+Kafka+Storm ...