题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5857
题意:给定n个数和m个时刻,i=1,从1时刻开始每时刻添加1个数,每当当前时刻在m个给定时间中时,执行一次get操作,即输出数列中第i小的数,i++
题解:https://blog.csdn.net/yeguxin/article/details/47258241
类似求第k小数这种题,可以考虑大顶堆(优先队列)q2,q2里面尽量装i个数,这i个数是当前所有数里面最小的,则第i小的数直接q2.top()即可,不够i个数则去剩下的数里面找,剩下的数用小顶堆排即可。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int maxn =30010;
int n, m;
int a[maxn], b[maxn];
int cnt;
int main() {
while(scanf("%d%d", &n, &m) != EOF) {
for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
priority_queue<int ,vector<int>, greater<int> > q1;
priority_queue<int ,vector<int>, less<int> > q2;
while(!q1.empty())
{
q1.pop();
}
while(!q2.empty())
{
q2.pop();
}
int ans = 0;
for(int i = 1; i <= m; i++) {
scanf("%d", &cnt);
for(int j = ans + 1; j <= cnt; j++) {
if(q2.size() < i) q2.push(a[j]);
else if(a[j] < q2.top()) {
q1.push(q2.top());
q2.pop();
q2.push(a[j]);
} else {
q1.push(a[j]);
}
}
if(q2.size() < i) {
int num = i - q2.size();
for(int k = 1; k < num; k++) {
b[k] = q1.top();
q1.pop();
}
printf("%d\n", q1.top());
for(int k = 1; k < num; k++) {
q1.push(b[k]);
}
} else
printf("%d\n", q2.top());
ans = cnt;
if(!q1.empty()) {
q2.push(q1.top());
q1.pop();
}
}
}
return 0;
}