Black Box
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 7592 | Accepted: 3121 |
Description
Our Black Box represents a primitive database. It can save an integer array and has 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 are two types of transactions:
ADD (x): put element x into Black Box;
GET: increase i by 1 and give an i-minimum out of all integers containing in the Black Box. Keep in mind that 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 11 transactions:
Example 1
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 two integer arrays:
1. A(1), A(2), ..., A(M): a sequence of elements which are being included into Black Box. A values are integers not exceeding 2 000 000 000 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 are 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 <= M and for each p (1 <= p <= N) an inequality p <= u(p) <= M is valid. It follows from the fact that for the p-element of our u sequence we perform a GET transaction giving p-minimum number from our A(1), A(2), ..., A(u(p)) sequence.
ADD (x): put element x into Black Box;
GET: increase i by 1 and give an i-minimum out of all integers containing in the Black Box. Keep in mind that 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 11 transactions:
Example 1
N Transaction i Black Box contents after transaction Answer (elements are 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 10 GET 4 -1000, -4, 1, 2, 3, 8 2 11 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 two integer arrays:
1. A(1), A(2), ..., A(M): a sequence of elements which are being included into Black Box. A values are integers not exceeding 2 000 000 000 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 are 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 <= M and for each p (1 <= p <= N) an inequality p <= u(p) <= M is valid. It follows from the fact that for the p-element of our u sequence we perform a GET transaction giving p-minimum number from 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 are 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 each line.
Sample Input
7 4 3 1 -4 2 8 -1000 2 1 2 6 6
Sample Output
3 3 1 2
/*
这道题用两个堆/*这个地方比较难想*/(用STL的优先队列可减少大量的代码)来实现:
一个小根堆,一个大根堆。始终维护这两个堆使大根堆中的所有的数均小于小根堆中的所有的数,且使小根堆的堆顶为第k小的数,大根堆中存储前k-1小的数即可,需要GET时只需打印小根堆的堆顶即可。
其余的看代码
//在做题过程中给代码写上注释方便梳理清楚自己的思路,小心做了后面的忘了前面的思路/*尤其是思维要求较大或代码量过大的*/,好习惯要学会培养
<pre name="code" class="cpp"><pre name="code" class="cpp"><pre name="code" class="cpp">#include<cstdio> #include<iostream> #include<cstring> #include<queue> using namespace std; /* 想了很久没啥思路,再看了下题,题中要求第 i 小的数. 于是就想到了小根堆和大根堆 就开了两个优先队列(STL还是挺好用的) //没手写堆了,代码量惹不起——! */ priority_queue<int> maxq;//从大到小 priority_queue<int, vector<int>, greater<int> > minq;//从小到大 int a[30010],u[30010]; int n,m,x,ans; int main() { scanf("%d%d",&m,&n); for(int i=1;i<=m;i++) scanf("%d",&a[i]); for(int j=1;j<=n;j++) scanf("%d",&u[j]); int i=0; int j=1,k=1; while(j<=n) { if(i==u[j]) //弹出第k小的数 { j++; if(maxq.size()<k) { x=minq.top(); maxq.push(x); minq.pop(); } ans=maxq.top(); printf("%d\n",ans); k++; //每次弹出一个数后,k的值都要加1 } else { i++; //maxq里的元素不够k个 if(maxq.size()<k) { minq.push(a[i]); x=minq.top(); minq.pop(); maxq.push(x);//先把a[i]压入minq,再从minq里取出最小值,压入maxq } /* 如果maxq的元素达到k个且要压入队列的值比maxq中的当前最大值大 说明maxq中当前的最大值并不是第k小 */ else if(maxq.top()>a[i]) { x=maxq.top(); maxq.pop(); minq.push(x); maxq.push(a[i]); } /* maxq中的元素个数达到k个且要压入队列的值比maxq中的当前最大值小 说明maxq中当前的最大值就是是第k小 则把a[i]直接压入minq中 */ else minq.push(a[i]); } } return 0; }