题目

P1801 黑匣子

简化题意

给你一堆数,每次让你求前多少个数中的第 \(k\) 小的数。

思路

大根堆 +小根堆。

没有询问前把数都加到大根堆里,有询问的时候当大根堆里的元素不少于 \(k\) 的时候就把堆顶的元素放到小跟堆里,最后小根堆堆顶的元素就是答案,最后要把小根堆堆顶的元素放回大根堆中。

Code

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#define MAXN 200001

int n, m, a[MAXN], b[MAXN];
std::priority_queue<int> q1;
std::priority_queue<int, std::vector<int>, std::greater<int> > q2;

int main() {
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
    for (int i = 1; i <= m; ++i) scanf("%d", &b[i]);
    int now = 1, cnt = 0;
    for (int i = 1; i <= b[now]; ++i) {
        q1.push(a[i]);
        while (i == b[now]) {
            ++cnt;
            while(q1.size() >= cnt) {
                int x = q1.top(); q1.pop();
                q2.push(x);
            }
            int ans = q2.top();
            q1.push(ans), q2.pop();
            printf("%d\n", ans);
            ++now;
        }
    }
    return 0;
}

相关文章:

  • 2021-10-03
  • 2022-01-20
  • 2021-07-16
  • 2019-12-22
  • 2021-12-02
  • 2022-01-10
  • 2021-09-14
猜你喜欢
  • 2021-09-22
  • 2021-05-26
  • 2021-07-26
  • 2021-06-22
  • 2022-02-05
  • 2021-07-27
  • 2022-01-10
相关资源
相似解决方案