UVA 501 Black Box

本文介绍了一种使用两个优先队列实现的算法,用于解决特定数值查询问题。该算法能够高效地处理一系列数值操作,通过维护两个优先队列来保持数据的有序状态,并能快速查询特定位置的数值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

优先队列模拟。输入n个数,然后是m次操作,第i个操作k是要求输出前k个数中第i大的数的值。

两个优先队列,可以理解为把前k个数由小到大排序,从第i个数后断开,第一段的最后一个就是所求啦。不妨把第一段反过来,用最大堆来存储,这样顶即为所求。每个询问操作都要先把第二段的第一个(最小值,用最小堆存储)压入第一段中,之后再判断第二段的最小值是否比第一段的最大值小(是否符合不降序),小就交换。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<string>
#include<queue>
#include<cmath>
#include<set>
#include<map>
///LOOP
#define REP(i, n) for(int i = 0; i < n; i++)
#define FF(i, a, b) for(int i = a; i < b; i++)
#define FFF(i, a, b) for(int i = a; i <= b; i++)
#define FD(i, a, b) for(int i = a - 1; i >= b; i--)
#define FDD(i, a, b) for(int i = a; i >= b; i--)
///INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)
#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)
#define RFI(n) scanf("%lf", &n)
#define RFII(n, m) scanf("%lf%lf", &n, &m)
#define RFIII(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define RFIV(n, m, k, p) scanf("%lf%lf%lf%lf", &n, &m, &k, &p)
#define RS(s) scanf("%s", s)
///OUTPUT
#define PN printf("\n")
#define PI(n) printf("%d\n", n)
#define PIS(n) printf("%d ", n)
#define PS(s) printf("%s\n", s)
#define PSS(s) printf("%s ", n)
///OTHER
#define PB(x) push_back(x)
#define CLR(a, b) memset(a, b, sizeof(a))
#define CPY(a, b) memcpy(a, b, sizeof(b))
#define display(A, n, m) {REP(i, n){REP(j, m)PIS(A[i][j]);PN;}}

using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int MOD = 100000000;
const int INFI = 1e9 * 2;
const LL LINFI = 1e17;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int N = 33333;
const int M = 1111111;
const int move[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1};

int a[N];
priority_queue<int> qb;
priority_queue<int, vector<int>, greater<int> > qs;

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    int t, n, m, k, p, x, y;
    RI(t);
    while(t--)
    {
        RII(n, m);
        REP(i, n)RI(a[i]);
        while(!qb.empty())qb.pop();
        while(!qs.empty())qs.pop();
        p = 0;
        REP(i, m)
        {
            RI(k);
            FF(j, p, k)qs.push(a[j]);
            x = qs.top();
            qs.pop();
            qb.push(x);
            while(!qs.empty() && qb.top() > qs.top())
            {
                x = qb.top();
                y = qs.top();
                qb.pop();
                qs.pop();
                qb.push(y);
                qs.push(x);
            }
            PI(qb.top());
            p = k;
        }
        if(t)PN;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值