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

Here is a detailed introduction

There is one coding test I'll give you a int n, a string float Number of numbers , I want you to print the current number in real time to the previous number n This is the number n The maximum of the number , No, n The number is the maximum of the first few numbers . You can use it here cartesian tree,label Records are subscripts of numbers ,p Represents the value . The insertion operation is lg(n), The deletion operation is also lg(n), The total complexity is 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 More articles about

  1. PAT-1167(Cartesian Tree) Rebuild the minimal heap from the middle order traversal sequence

    Cartesian Tree PAT-1167 At first I used arrays for storage , But this may result in not opening a large enough array , Because if the tree is a linked list, it can't open such a large array ( Although there are few nodes ). The right solution still needs to be achieved , Use ...

  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

    Preface I've been working on the topic recently , Cartesian trees have been used more than once . This data structure is excellent , But the details of the construction are very error prone . So write an article to make a summary . Cartesian tree  Cartesian Tree Introducing problems Yes N A long rectangular bar , Width all ...

  4. PAT-2019 The winter exam - Class A 7-4 Cartesian Tree (30 branch )( The middle order traversal of the minimum heap is to find the sequence traversal , Recursive tree building bfs sequence )

    7-4 Cartesian Tree (30 branch )   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 —— Cartesian tree

    [ Topic analysis ] Construct a Cartesian tree , Then output the tree . So let's sort it first , Then a stack is used to maintain the node information of the rightmost tree , Insert according to the second keyword to find , Find it and insert it , The tree below can be its left subtree . Then we discuss it in three situations ...

  7. SGU 155.Cartesian Tree

    The time limit :0.25s Space restriction :6M The question : give n(n< 50000) Two with double keywords (key,val) The node of , Construct a tree so that it , Press key Value is a binary search tree , Press val Value is a small heap of roots . Soluti ...

  8. OpenJudge Cartesian Tree

    [ Code ] #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 ...

Random recommendation

  1. Cash Cow【dfs More difficult applications 】【sdut2721】

    Cash Cow Time Limit: 1000ms   Memory limit: 65536K   Have a question ? Click here ^_^ Title Description Topic link :http://acm.sdut.edu.cn/sdut ...

  2. stay Ubuntu 64 position OS Up operation hadoop2.2.0[ recompile hadoop]

    I've been learning to build Hadoop, We from Apache Download the latest version directly from the official website Hadoop2.2. At present, the government has provided linux32 Bit system executable , Results the runtime found prompt “libhadoop.so.1.0.0 ...

  3. poj2540Hotter Colder( Half plane intersection )

    link According to the distance, we can get the linear equation , Attach the four vertices of the initial rectangle , Cut in a straight line in turn . #include<iostream> #include <stdio.h> #include < ...

  4. common SQLException abnormal

    ORA-00904:  invalid column name Invalid column name ORA-00942:  table or view does not exist The table or view does not exist ORA-01400:  c ...

  5. SQL Server Batch update data

    There is a location in the project where tens of thousands of pieces of data need to be inserted in batches , Batch insert The waiting time is just ... use SqlBulkCopy after , Previous needs 1 Minute or so sql Now in the blink of an eye ( It's really just a blink of an eye ) Later this function will be added to another project , in addition ...

  6. hadoop-0.20- Cluster building ___ The physical machine passes through SSH Access based on VM Installed Linux

    Have to say LZ In the beginning, build hadoop When , because VM Network segment configuration and local IP The address is not configured , So it's been used all the time VM The shared folder function of , So that after the cluster is built , Only namenode The host can realize the function of sharing ...

  7. Mysql primary key 、 unique index 、 General index 、 Full-text index 、 The difference between composite indexes

    original text :Mysql primary key . unique index . General index . Full-text index . The difference between composite indexes Mysql Index concept : say something Mysql Indexes , See a few, like : An index is like a catalogue of a book , It allows you to find content faster , Obviously, the directory ( Indexes ) Does not ...

  8. [js Master's Road ] Step by step javascript The prototype of the (prototype) object , Prototype chain

    Let's move on , We do it in a prototype way , The method sharing problem of multiple instances is solved , Next , Let's figure out the prototype (prototype), The origin and development of the prototype chain . function CreateObj(uName) { this ...

  9. Linux Upload local files Linux The server , namely ssh Command to upload local files

    utilize ssh Transfer files   stay linux I usually use scp This order comes through ssh Transfer files . 1. Download files from the server scp username@servername:/path/filename /var/www ...

  10. In the third quarter java Array ( Loop traversal 、 Get the maximum value of an array ( Maximum and minimum )、 Selection sort 、 Bubble sort 、 Practice outputting uppercase A)

    Get the maximum value of an array ( Maximum and minimum ) Ideas : 1. Getting the best value requires comparison , Each comparison has a larger value , Because it's time to Value uncertainty , You need a variable for temporary storage . 2. Let each element in the array be compared with the value in the variable , If it is greater than ...