uva 501 - Black Box(优先队列)

题目链接:uva 501 - Black Box

题目大意:有一个集合,给定元素进入集合的顺序,现在有Q次查询,给定每次查询在第几个元素进入集合后,对于每i次查询,输出集合中第i小的数。

解题思路:用两个优先队列维护,队列a优先出值大的,队列b优先出值小的,在第i次询问前,保证a队列中有i-1个元素元素,并且抱枕都比b中的小,然后每次询问输出b队列的首元素,并且将它放到a队列中。

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>

using namespace std;
const int maxn = 30005;

int N, M, arr[maxn], v[maxn];

void solve () {
    priority_queue<int> a;
    priority_queue<int, vector<int>, greater<int> > b;

    for (int i = 1; i <= N; i++) {
        a.push(arr[i]);
        b.push(a.top());
        a.pop();

        while (v[i]--) {
            printf("%d\n", b.top());
            a.push(b.top());
            b.pop();
        }
    }
}

int main () {
    int cas;
    scanf("%d", &cas);
    while (cas--) {
        int x;
        memset(v, 0, sizeof(v));
        scanf("%d%d", &N, &M);

        for (int i = 1; i <= N; i++)
            scanf("%d", &arr[i]);
        for (int i = 0; i < M; i++) {
            scanf("%d", &x);
            v[x]++;
        }

        solve();

        if (cas)
            printf("\n");
    }
    return 0;
}
时间: 2024-08-05 02:25:51

uva 501 - Black Box(优先队列)的相关文章

UVA - 501 Black Box (优先队列或vector)

Description  Black Box  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).

POJ 1442 Black Box(优先队列)

题目地址:POJ 1442 这题是用了两个优先队列,其中一个是较大优先,另一个是较小优先.让较大优先的队列保持k个.每次输出较大优先队列的队头. 每次取出一个数之后,都要先进行判断,如果这个数比较大优先的队列的队头要小,就让它加入这个队列,队列头移到较小优先的队列中.然后当较大优先的数不足k个的时候,就让较小优先的队列的队头移到较大优先的队头中. 代码如下: #include <iostream> #include <cstdio> #include <string>

uva 10537 Toll! Revisited(优先队列优化dijstra及变形)

Toll! Revisited 大致题意:有两种节点,一种是大写字母,一种是小写字母.首先输入m条边,当经过小写字母时需要付一单位的过路费,当经过大写字母时,要付当前财务的1/20做过路费.问在起点最少需要带多少物品使到达终点时还有k个物品.当有多条符合条件的路径时输出字典序最小的一个. 思路:已知终点的权值,那么可以从终点向前推.求终点到起点的最短路径,然后按字典序打印路径. 比较难处理的是:向前推时前驱节点的权值计算.列个方程算算就可以了,主要时不能整除的情况. 计算前驱结点dis值的时候,

UVa 136 Ugly Numbers(优先队列)

题意  质因数只可能有2,3,5的数称为丑数  输出第1500个丑数 STL优队列应用  1是丑数 丑数的2,3,5倍都是丑数  用优先队列模拟就行了 #include <cstdio> #include <cstring> #include <set> #include <queue> using namespace std; typedef long long ll; //priority_queue<ll, vector<ll>, g

POJ 1442-Black Box(优先队列)

Black Box Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7436   Accepted: 3050 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 empt

UVA 11997--K Smallest Sums+优先队列用于多路归并

题目链接:点击进入 先考虑两个数组A,B的情况,这样总共有n^2种情况;将A,B数组排序后,我们可以将所有情况组织成n张表: 表1: A[1]+B[1]<=A[1]+B[2]<=--<=A[1]+B[n]. 表2: A[2]+B[1]<=A[2]+B[2]<=--.<=A[2]+B[n]. --. 表n: A[n]+B[1]<=A[n]+B[2]<=--..<=A[n]+B[n] 这n张表都是有序的,所以可以以多路归并的思想求出其中前n个最小的元素.对

UVA 4855 Hyper Box

You live in the universe X where all the physical laws and constants are different from ours. For example all of their objects are N-dimensional. The living beings of the universe X want to build an N-dimensional monument. We can consider this N dime

UVA 4855 Hyper Box 斐波那契

题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2856 比赛的时候和陈看了好长时间才看明白题意,和吴讨论做了出来,其实吴确实聪明,脑袋瓜子比较灵活 题意:有一个N维的空间,给你各维的长度,有一些n维的砖块,把空间填满,砖块不能交叉,空间不能一部分为空:求最少用的砖块 砖块各维都必须为斐波那契长度 思路: 空间各维分

UVA 11573 - Ocean Currents【BFS+优先队列】

题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2620 题意:给定一个海面,数字分别代表海流方向,顺着海流不用费能量,逆海流要费1点能量,每次询问给一个起点一个终点,问起点到终点耗费的最小能量 思路:广搜,队列用优先队列,每次取能量最低的点. 代码: #include <stdio.h> #include <