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
N Transaction I Black Box contents after Transaction Answer (elements is arranged by non-descending) 1 ADD (3)
0 3 2 GET 1 3 3 3 Add (1) 1 1, 3 4 GET 2 1, 3 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 1, GET 4-1000, -4, 1, 2, 3, 8 2, 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
#include <cstdio> #include <queue>using namespace Std;int main () {int a[30005],i,j,n,m; while (scanf ("%d%d", &n,&m)!=eof) {priority_queue <int, vector <int>, less<int> > P;//Large First priority_queue <int, Vector <int>, greater<int> >q;//Small First int cut=0,x,c=0,t; for (i=0; i<n; i++) {scanf ("%d", &a[i]); } for (i=0; i<m; i++) {scanf ("%d", &x); while (c<x) {Q.push (a[c]); C + +; } while (!p.empty () &&p.top () >q.top ())//guarantee the number of small numbers at the top of the Q queue or P queue, and then care about the size of the two {T =p.top (); P.pop (); P.push (Q.top ()); Q.pop (); Q.push (t); } printf ("%d\n", Q.top ()); P.push (Q.top ()); Q.pop (); }} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
poj1442 Black Box "Priority queue, define two queues"