https://www.acwing.com/activity/content/problem/content/397/1/
我们的大根堆中的所有数都必须小于小根堆中的数。而且还要注意,i每一次都是要增加1的,所以我们每次做完Get操作后,都需要最小堆中的最小数弹出存入最大堆,也就是满足第K小,因为K就是i,所以我们要这么处理。
俩个堆维护一个有序序列,求第k个小的数
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int N = 30010;
int n, m;
int a[N], b[N];
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i ++ ) cin >> a[i];
for (int i = 1; i <= m; i ++ ) cin >> b[i];
priority_queue<int> down;
priority_queue<int, vector<int>, greater<int>> up;
int k = 0;
for (int i = 1, j = 1; i <= m; i ++ )
{
while (j <= b[i])
{
if (down.empty() || a[j] >= down.top()) up.push(a[j]);
else
{
up.push(down.top());
down.pop();
down.push(a[j]);
}
j ++ ;
}
down.push(up.top());
up.pop();
cout << down.top() << endl;
}
return 0;
}