Black box--[priority queue, maximum heap minimum heap application]

Source: Internet
Author: User

Description

Our Black Box represents a primitive database. It can save an integer array and have a special I variable. At the initial moment Black Box is empty and I equals 0. This Black Box processes a sequence of commands (transactions). There is types of transactions:

ADD (x): Put element x into Black Box;
Get:increase I by 1 and give a i-minimum out of all integers containing in the Black Box. Keep in mind this i-minimum is a number located at I-th Place after Black Box elements sorting by non-descending.

Let us examine a possible sequence of one transactions:

Example 1


(Elements is arranged by non-descending)
1 ADD (3)
2 GET 1 3
3 ADD (1)
4 GET 2 1, 3
5 ADD (-4)
6 ADD (2)
7 ADD (8)
8 ADD (-1000)
9 GET 3-1000,-4, 1, 2, 3, 8
Ten GET 4-1000,-4, 1, 2, 3, 8
ADD (2) 4-1000,-4, 1, 2, 2, 3, 8

It is required to work out an efficient algorithm which treats a given sequence of transactions. The maximum number of ADD and GET transactions:30000 of each type.


Let us describe the sequence of transactions by both integer arrays:


1. A (1), A (2), ..., A (M): A sequence of elements which is being included into Black Box. A values is integers not exceeding 2 by their absolute value, M <= 30000. For the Example we have a= (3, 1,-4, 2, 8,-1000, 2).

2. U (1), U (2), ..., U (N): A sequence setting a number of elements which is being included into Black Box at the moment of First, second, ... and N-transaction GET. For the Example we have u= (1, 2, 6, 6).

The Black Box algorithm supposes that natural number sequence U (1), U (2), ..., u (N) is sorted in non-descending order, N & lt;= m and for each p (1 <= P <= N) a inequality P <= u (p) <= m is valid. It follows from the fact so for the p-element of our U sequence we perform a GET transaction giving P-minimum number fro M our A (1), A (2), ..., A (U (p)) sequence.


Input

Input contains (in given order): M, N, a (1), A (2), ..., a (M), U (1), U (2), ..., U (N). All numbers is divided by spaces and (or) carriage return characters.

Output

Write to the output Black Box answers sequence for a given sequence of transactions, one number from each line.

Sample Input

7 43 1-4 2 8-1000 21 2 6 6

Sample Output

3312

Analysis of problem-solving ideas:
The first thought to get this problem is ( error ):
1. Save the Add command with a queue and save the element into the black box with a vector.
2. Each time the element is added to the black box, the elements in the black box are sorted, and the first element is output.
This algorithm seems to be useless, but in fact, after extreme data testing, when M, N are 30000, run the program needs 2.7s, will time out, because every time the element is added to order all elements, the time overhead is too large. However, after analysis is easy to find, it is not necessary to order all the elements each time, we only need to save the first i-1 small elements, and then find the first element after the smallest element and the first i-1 elements of the largest element comparison, you can get the small I element. This way of thinking is clear,
Here's the right idea :
1. The minimum heap (top minimum) and maximum heap (top maximum) are established with priority queue respectively;
2. The minimum heap top element is added to the maximum heap each time I increment;
3. Compare the minimum heap top element and the maximum heap top element each time the element is added, and if top small > top large, swap two heap top elements;
4. Output the minimum heap top element.
Note the two usage of the priority queue:

1. The standard library uses the < operator of the element type by default to determine the precedence relationship between them.

Priority_queue<int> Q;

The < operator shows the high precedence of the elements in integers.
2. The smaller the data, the higher the priority greater<int> defined in the header file <functional>

3. Custom comparison Operators <

The code is as follows:
1#include <iostream>2#include <Set>3#include <vector>4#include <cstdio>5#include <queue>6#include <algorithm>7#include <time.h>8#include <functional>9 using namespacestd;Ten #defineclock__ (double (Clock ())/clocks_per_sec) One  A intM,n; -queue<int> A;//Store Add Command -priority_queue<int,vector<int>,greater<int> > a_min;//Minimum Heap thepriority_queue<int> A_max;//Maximum Heap - inti; -  - intMain () { + -I=0; +scanf"%d%d",&m,&N); A      while(m--) { at         intA; -scanf"%d",&a); - A.push (A); -     } -      while(n--){ -         intu; inscanf"%d",&u); -i++; to         if(A_min.size ()! =0) { + A_max.push (A_min.top ()); - A_min.pop (); the         } *         intn=u-(a_min.size () +a_max.size ()); $          while(n--){Panax Notoginseng A_min.push (A.front ()); A.pop (); -             if(A_max.size () &&a_min.size () &&a_min.top () <A_max.top ()) { the                 intA=A_max.top (); A_max.pop (); +                 intb=A_min.top (); A_min.pop (); A A_min.push (A); A_max.push (b); the             } +         } -printf"%d\n", A_min.top ()); $          $     } -    //cout<< "Time:" <<clock__<<endl; -     return 0; the}


  

Black box--[Priority queue, maximum heap minimum heap application]

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.