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. android 开发Parcelable 怎么传值ArrayList

    public class TradeEntity implements Parcelable{ public String id; //有关进度条的参数 ArrayList<TradeState ...

  2. android开发 解决启动页空白或黑屏问题

    遇到的情况: app启动时进入启动页时出现白屏页,然后大概一秒之后就出现了背景图片. 原因:app启动时加载的是windows背景,之后再加载布局文件的,所以开始的黑屏/白屏就是windows的背景颜 ...

  3. 【Permutations II】cpp

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  4. SL410K 在Ubuntu禁用触摸板

    由于之前把系统自带的恢复去了,然后TouchPad一直不能禁用,而后我的410k就只装上ubuntu,想不到在ubuntu上,禁用/启用 触摸板这么方便. http://askubuntu.com/q ...

  5. javascript_04 数据类型

    ECMAScript 数据类型  标准  核心   数据类型 typeof  判断数据类型 数字型 布尔型 对象类型 函数 字符串 undefined 未定义 数字型  字符型 var s='1233 ...

  6. WWDC2014 debugging_in_xcode_6 总结

    1.GCD的waiting queue 可以在Xcode左侧的面板中查看. 2.可以对View进行debug类似reveal. 3.查看运行时的对象,对于自定义对象可以实现- (id)debugQui ...

  7. Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树

    题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...

  8. Codeforces Round #354 (Div. 2) C. Vasya and String

    题目链接: http://codeforces.com/contest/676/problem/C 题解: 把连续的一段压缩成一个数,对新的数组求前缀和,用两个指针从左到右线性扫一遍. 一段值改变一部 ...

  9. C实现面向对象封装、继承、多态

    参考资料:      http://blog.chinaunix.net/uid-26750235-id-3102371.html      http://www.eventhelix.com/rea ...

  10. c++ union

    什么是union? 翻译过来说,就是共用体,或者也叫联合体.说到了union,也就是共用体,就不得不说一下struct了,当我们有如下的struct的定义时:   1 2 3 4 5 6 struct ...