Black Box_ Double Priority queue

Source: Internet
Author: User

Black boxtimelimit:1000ms memorylimit:10000kb64-bit integer IO format:%lldProblem Descriptionour 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) 0 3
2 GET 1 3
3 ADD (1) 1 1, 3
4 GET 2 1, 3
5 ADD ( -4) 2-4, 1, 3
6 ADD (2) 2-4, 1, 2, 3
7 ADD (8) 2-4, 1, 2, 3, 8
8 ADD ( -1000) 2-1000,-4, 1, 2, 3, 8
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.


Inputinput 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. Outputwrite to the output Black Box answers sequence for a given sequence of transactions, one number each line. Sampleinput
7 43 1-4 2 8-1000 21 2 6 6
Sampleoutput
3312

There are two priority queues, the maximum heap and the minimum heap, respectively, with the aim of obtaining the number of numbers in N.
Code
#include <cstdio>#include<algorithm>#include<queue>#include<vector>using namespacestd;voidSolveint,int );intMainvoid){    //freopen ("D://test.txt "," rw+ ", stdin);    intN, t;  while(SCANF ("%d%d", &n, &t)! =EOF)    {Solve (n, t); }    return 0;}voidSolveintNintt) {    int*_m =New int[n+1 ]; intnum; Priority_queue<int, vector<int, Greater <int> >pri_min; Priority_queue<int, vector<int, Less <int> >Pri_max;  for(intI=0; I < n; ++i) {scanf ("%d", _m+i); }     for(intI=1, j=0; T--; ++i) {scanf ("%d", &num); //quickly fills to a specified number of numbers         while(Pri_max.size () +pri_min.size () <num) {Pri_min.push (_m[J++ ] ); }        //quickly fills to the maximum heap so that the maximum number of heaps is I         while(Pri_max.size () <i)            {Pri_max.push (Pri_min.top ());        Pri_min.pop (); }        //swaps the maximum heap and minimum heap data until it meets the conditions         while(Pri_min.size () && pri_max.top () >Pri_min.top ()) {num=Pri_max.top ();            Pri_max.pop ();            Pri_max.push (Pri_min.top ());            Pri_min.pop ();        Pri_min.push (num); } printf ("%d\n", Pri_max.top ()); }    Delete[] _m;}
View Code

Black Box_ Double Priority queue

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.