Poj 1442 heap operations

Source: Internet
Author: User

Question:
Two operations are provided: ADD (x), ADD x to the ordered list; GET () returns the value referred to by the global iterator. After the GET operation, the iterator automatically adds 1
Question:
At the beginning, I directly played the linked list simulation, and the result timed out. In this case, another method should be used: Large top heap and small top heap.
For the series S [1. n] and the index representing the iterator position, the big top heap maintains the sorted S [1 .. index-1], and the small top heap maintains
Sorted S [index .. n], for example, S [1 .. n] =, 7, index = 4, the top heap is {, 3}, and the top heap is {, 6, 7}
Why is Maintenance like this? Because when the smallest element of a small heap is greater than the largest element of a large heap, the nth number in the sequence is the smallest number of small heap.
Let's assume that after the k-GET (), there are the following scenarios (k automatically adds 1 after GET ):
Big Top heap: S [1 .. k]. The heap top element is S [k], the small top heap is S [k + 1, n], and the heap top element is S [k + 1]. then, each time a newE element is added to the Big Top heap, if the number of large top heap is greater than the number of small top heap, it should be exchanged.
Code: # include <queue>
# Include <stdio. h>
Using namespace std;

Int m, n;
Int sequence [30005];

Struct cmp1
{
Bool operator () (const int a, const int B)
{
Return a> B;
}
};
Struct cmp2
{
Bool operator () (const int a, const int B)
{
Return a <B;
}
};

Void Test ()
{
Priority_queue <int, vector <int>, cmp1> q1; // small heap
Priority_queue <int, vector <int>, cmp2> q2; // large heap

For (int I = 0; I <m; ++ I)
{
Scanf ("% d", & sequence [I]);
}
Int op;
Int k = 0;
For (int I = 0; I <n; ++ I)
{
Scanf ("% d", & op );
While (k <op)
{
Q1.push (sequence [k]);
If (! Q2.empty () & q1.top () <q2.top ())
{
Int t1 = q1.top ();
Q1.pop ();
Int t2 = q2.top ();
Q2.pop ();
Q1.push (t2 );
Q2.push (t1 );
}
++ K;
}
Printf ("% d", q1.top ());
Q2.push (q1.top ());
Q1.pop ();
}

}

Int main ()
{
While (scanf ("% d", & m, & n )! = EOF)
{
Test ();
}
Return 0;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.