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
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:
-
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).
-
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
问题分析与解题思路
题意
输入一行数A[];再输入一行数u[];让你输出前[]的前u[p]个数据中,第p+1小的数,正如给出的例子
7 4
3 1 -4 2 8 -1000 2
1 2 6 6
前1个数中第1小的数是3;
前2个数中第2小的数是3;
前6个数中第3小的数是1;
前6个数中第4小的数是2;
通过维护一个最大堆,一个最小堆进行求解。
- 最大堆中存放第1到第i-1小的数
- 最小堆中存放第i-1到第n小的数(n为当前数字总数)
所以最小堆的堆顶就是我们要的结果。
每插入一个数,我们先把它放入最小堆,更新最小堆,如果最小堆堆顶元素小于最大堆堆顶元素,说明前i-1个最小的数据需要更新,只要将小堆堆顶和大堆堆顶元素交换即可。再把最小堆的堆顶元素弹出输出,然后赋值给最大堆堆顶。(因为下一次所求不是第i小而是第i+1小的元素)
数据结构与算法设计及其主要代码段
定义堆
priority_queue<int> q2; //最大堆(默认);
priority_queue<int,vector<int>,greater<int> > q1;//最小堆(自定义)
插入与get
for(i=0;i<m;i++)
{
scanf("%d",&k);
while(j<k)
{
q1.push(A[j]);
// 最小堆堆顶元素小于最大堆堆顶元素
if(!q2.empty() && q1.top()<q2.top())
{
int t;
t=q1.top();
q1.pop();
q1.push(q2.top());
q2.pop();
q2.push(t);
}
j++;
}
// 碰到get弹出输出最小堆堆顶,并加入最大堆
printf("%d\n",q1.top());
q2.push(q1.top());
q1.pop();
}
程序运行结果及分析
A. 算法复杂度
- 建堆:O(n)
- 插入删除:O(logn)
- 总的复杂度:O(n)
B. 运行时间
内存:2260kB, 时间:52ms(数据来自openjudge)
心得体会与总结
- 使用stl的priority_queue会让本题难度降低很多。
- 本题最重要的就是想清楚两个堆中所存的元素,最大堆中存放第1到第i-1小的数,最小堆中存放第i-1到第n小的数(n为当前数字总数。
- 本题巧妙之处在于,一般堆都是用来解决最大,最小问题,两个堆的使用可以解决第k大/小问题。
网友评论