Black Box
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 8637 |
|
Accepted: 3542 |
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) 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.
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
Main topic:
There are several sets of test data, the first line input n,q, indicating that there are n data to be entered, there is Q a query.
The second line is the number of N, which indicates that there is a data set (n data exists).
The third line input Q number, set each digit QI, the first digit QI, the number of the input these data sets of the first Qi, to find out in these data set I small data;
Solution:
Since the small number of K for each inquiry is not fixed, k is incremented, can be used two heaps (small top heap and large top heap) to achieve data maintenance,
1. we want to make sure that each element of the small top heap is larger than every element in the big top heap.
2. ensure that there are k elements in the large top heap (from the top heap to the top of the heap, until the big top heap has k elements), the large top heap of the heap top of the element is not so data of the nth small data.
3. Maintenance , it is only necessary to determine the top of the heap of the top of the heap is larger than the top element of the heap, if greater than the above, no more exchange, if less than, need to exchange two heap top elements and then repeated judgment, until, the heap of the small top heap of heap top elements are larger than the top of the heap top elements.
1#include <iostream>2#include <queue>3#include <stdio.h>4 #defineMAX 300105 using namespacestd;6 structNode17 {8 intS;9FriendBOOL operator<(Node1 a,node1 b)Ten{returnA.s>B.S;} One }; A structNode2 - { - intS; theFriendBOOL operator<(Node2 a,node2 b) -{returna.s<B.S;} - }; - intMain () + { - intn,m,i,j,k,q,a,b,sign,t=1; + Node1 Num1; A Node2 Num2; at intNum[max]; -priority_queue<node1>id1;/*Small Top Pile*/ -priority_queue<node2>id2;/*Big Top Pile*/ - while(SCANF ("%d%d", &n,&m)! =EOF) - { - for(i=0; i<n;i++) in { -scanf"%d",&num[i]); to } +sign=1; j=0; - for(i=0; i<m;i++) the { *scanf"%d",&Q); $ while(j<q&&j<N)Panax Notoginseng{/*Add a small top heap element*/ -num1.s=num[j++]; the Id1.push (NUM1); + } A for(K=id2.size (); k<sign;k++) the{/*ask for a small number of k, to ensure that the large top heap has K number*/ +num2.s=id1.top (). S;id1.pop (); - Id2.push (Num2); $ } $ - while(Id2.size () >0&&id1.top (). s<id2.top (). S) -{/*Maintenance*/ thenum1.s=id2.top (). S;id2.pop (); - Id1.push (NUM1);Wuyi thenum2.s=id1.top (). S;id1.pop (); - Id2.push (Num2); Wu } -printf"%d\n", Id2.top (). S); Aboutsign++; $ } - } - return 0; -}
View Code
"Priority queue-the number of ki large" Black Box