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大/小問題宅广。