http://baike.baidu.com/link?url=XUt5fXQ-jtFBM0UdKiGA41_NWFvdFSYwVsy4SVvCRRuEBvNkLfT9TgOtzsXvaOT9nuq_EzKJcO0gt6nyXRSLU_

这里有详细介绍

有一道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的更多相关文章

  1. PAT-1167(Cartesian Tree)根据中序遍历序列重建最小堆

    Cartesian Tree PAT-1167 一开始我使用数组进行存储,但是这样可能会导致无法开足够大的数组,因为树如果是链表状的则无法开这么大的数组(虽然结点很少). 正确的解法还是需要建树,使用 ...

  2. [sgu P155] Cartesian Tree

    155. Cartesian Tree time limit per test: 0.25 sec. memory limit per test: 65536 KB input: standard i ...

  3. 笛卡尔树Cartesian Tree

    前言 最近做题目,已经不止一次用到笛卡尔树了.这种数据结构极为优秀,但是构造的细节很容易出错.因此写一篇文章做一个总结. 笛卡尔树 Cartesian Tree 引入问题 有N条的长条状的矩形,宽度都 ...

  4. 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 ...

  5. 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 ...

  6. POJ 2201 Cartesian Tree ——笛卡尔树

    [题目分析] 构造一颗笛卡尔树,然后输出这棵树即可. 首先进行排序,然后用一个栈维护最右的树的节点信息,插入的时候按照第二关键字去找,找到之后插入,下面的树成为它的左子树即可. 然后插入分三种情况讨论 ...

  7. SGU 155.Cartesian Tree

    时间限制:0.25s 空间限制:6M 题意: 给出n(n< 50000)个含双关键字(key,val)的节点,构造一颗树使该树,按key值是一颗二分查找树,按val值是一个小根堆. Soluti ...

  8. OpenJudge Cartesian Tree

    [代码] #include <cstdio> #include <cstdlib> #include <cstring> #include <algorith ...

  9. [Algorithm] Binary tree: Level Order Traversal

    function Node(val) { return { val, left: null, right: null }; } function Tree() { return { root: nul ...

随机推荐

  1. 转载:Comet:基于 HTTP 长连接的“服务器推”技术

    转自:http://www.ibm.com/developerworks/cn/web/wa-lo-comet/ 很多应用譬如监控.即时通信.即时报价系统都需要将后台发生的变化实时传送到客户端而无须客 ...

  2. android开发,静音录制视频,在一般清晰度的前提下保证文件大小越小越好

    public void startRecord() { mediarecorder = new MediaRecorder();// 创建mediarecorder对象 mCamera = getCa ...

  3. 【Evaluate Reverse Polish Notation】cpp

    题目: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are + ...

  4. bnuoj 34985 Elegant String DP+矩阵快速幂

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 We define a kind of strings as elegant s ...

  5. The 15th Zhejiang University Programming Contest

    a  ZOJ 3860 求和大家不一样的那个数,签到,map水之 #include<cstdio> #include<map> using namespace std; map ...

  6. 偶然发现的一个地图网站mapbox

    https://www.mapbox.com/ 可以自定义地图,并放到dropbox中.时间有限,更多功能有待发现 可以用他的切片 http://a.tiles.mapbox.com/v3/jambo ...

  7. 云计算中iaas、paas、saas的区别和联系

    概念: iass : Infrastructure(基础设施)-as-a-Service, paas : Platform(平台)-as-a-Service, saas : Software(软件)- ...

  8. eclipse加速

    eclipse老是building workspace及自动更新问题,eclipse加速 最近用Eclipse开发oPhone的一个项目,每次打开Eclipse的时候,总是在build workspa ...

  9. **apache环境下 禁止显示 index of/ 目录下(如何禁止访问网站根目录)

    比如: http://123.57.49.XX6// 当这样访问的时候,可能会列出网站的根目录 如何禁止列出网站目录,方法如下: 让别人知道你的网站目录结构直接查看你目录下的所有文件是很危险的一个事情 ...

  10. Visual Studio 快捷键

    Visual Studio 快捷键 CTRL + DELETE 删除至词尾 CTRL + BACKSPACE 删除至词头Ctrl+Shift+L: 删除当前行 Ctrl+K+Crtr+C: 注释选定内 ...