[ACM] POJ 1442 Black Box (堆,优先队列)

[ACM] POJ 1442 Black Box (堆,优先队列)

大家好,又见面了,我是全栈君。

Black Box
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7099   Accepted: 2888

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 empty and i equals 0. This Black Box processes a sequence of commands (transactions). There are two types of transactions: 

ADD (x): put element x into Black Box; 

GET: increase i by 1 and give an i-minimum out of all integers containing in the Black Box. Keep in mind that i-minimum is a number located at i-th place after Black Box elements sorting by non- descending. 

Let us examine a possible sequence of 11 transactions: 

Example 1 

N Transaction i Black Box contents after transaction Answer 

      (elements are arranged by non-descending)   

1 ADD(3)      0 3   

2 GET         1 3                                    3 

3 ADD(1)      1 1, 3   

4 GET         2 1, 3                                 3 

5 ADD(-4)     2 -4, 1, 3   

6 ADD(2)      2 -4, 1, 2, 3   

7 ADD(8)      2 -4, 1, 2, 3, 8   

8 ADD(-1000)  2 -1000, -4, 1, 2, 3, 8   

9 GET         3 -1000, -4, 1, 2, 3, 8                1 

10 GET        4 -1000, -4, 1, 2, 3, 8                2 

11 ADD(2)     4 -1000, -4, 1, 2, 2, 3, 8   

It is required to work out an efficient algorithm which treats a given sequence of transactions. The maximum number of ADD and GET transactions: 30000 of each type. 

Let us describe the sequence of transactions by two integer arrays: 

1. A(1), A(2), …, A(M): a sequence of elements which are being included into Black Box. A values are integers not exceeding 2 000 000 000 by their absolute value, M <= 30000. For the Example we have A=(3, 1, -4, 2, 8, -1000, 2). 

2. u(1), u(2), …, u(N): a sequence setting a number of elements which are being included into Black Box at the moment of first, second, … and N-transaction GET. For the Example we have u=(1, 2, 6, 6). 

The Black Box algorithm supposes that natural number sequence u(1), u(2), …, u(N) is sorted in non-descending order, N <= M and for each p (1 <= p <= N) an inequality p <= u(p) <= M is valid. It follows from the fact that for the p-element of our u sequence we perform a GET transaction giving p-minimum number from our A(1), A(2), …, A(u(p)) sequence. 

Input

Input contains (in given order): M, N, A(1), A(2), …, A(M), u(1), u(2), …, u(N). All numbers are divided by spaces and (or) carriage return characters.

Output

Write to the output Black Box answers sequence for a given sequence of transactions, one number each line.

Sample Input

7 4
3 1 -4 2 8 -1000 2
1 2 6 6

Sample Output

3
3
1
2

Source

7 4 3 1 -4 2 8 -1000 2 1 2 6 6

7代表以下给定7个数的数字序列。4能够理解为四次查询, 1 2 6 6为查讯。第一次查询是求数字序列仅仅有前一个数(3)时。此时的第一小的数字,即1,第二次查询是求数字序列仅仅有前2个数(3 1)时。此时的第二小的数字,即 3,第三次查询是求数字序列仅仅有前6个数时(3,1,-4,2,8,-1000)。此时的第三小的数字,即1,第四次查询是求数字序列仅仅有前6个数时。此时的第四小的数字。

思路为: 当求第4小的数字时,我们用一个大顶堆来维护前三个最小的数字,那么小顶堆的堆顶即为所求。

用priority_queue<int>big;  优先队列还起到大顶堆的作用,顶部即为最大值  ,即 big.top();
    priority_queue<int,vector<int>,greater<int> >small;   小顶堆。顶堆为最小值 。即 small.top();

用例子来说明一下 大顶堆和小顶堆工作方法:

1 2 6 6

首先是1:  把第一个数3 插入到小顶堆中,这时候推断。假设大顶堆不为空且小顶堆的top小于大顶堆的top时,就把二者的top值互换,由于,大顶堆中的数不能比小顶堆中的大。大顶堆维护第i次查询时,前i-1个最小的数,这时候大顶堆为空,不用互换值。 输出第一次查询时前1个数的第1小的数即为 小顶堆的top 3,然后把小顶堆的top 移除。放到大顶堆中。

然后是2: 把第二个数1插入到小顶堆中。推断,大顶堆不为空,小顶.top() 1 <大顶.top() 3 ,所以二者互换,小顶.top()为3,大顶top()为1。 然后输出小顶.top(),即为第二小的数。把小顶的.top()移除,放入大顶中。这是大顶中维护的是眼下数字中前两个最小的数。

然后是6: 要求前6个数中第3小的数字,先得把3 1以后的四个数字插入才可以六个数。依次插入,  -4插入小顶堆。这时 大顶堆 3 1,-4<3,互换,  小顶堆 3,大顶堆为1 -4 ,2插入小顶堆,小顶堆 2,3  大顶堆 1 -4,  2>1,不用互换,  8插入到小顶堆。小顶堆为  2,3,8  大顶堆为 1。-4。  2>1,不用互换,-1000插入到小顶堆。小顶堆 -1000,2,3,8,大顶堆1,-4, -1000<1,不行,互换以后得小顶堆。1,2,3,8 , 大顶堆 -1000。-4 ,输出小顶堆.top() 1。 并把它移除放入到大顶堆中。为下次查询做准备。

所以从以上能够看出,关键点就是第i次查询时,大顶堆中维护的总是眼下数字中最小的 i-1个数。

代码:

#include <iostream>
#include <stdio.h>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn=30010;
int num[maxn];
int n,m;

int main()
{
    priority_queue<int>big;
    priority_queue<int,vector<int>,greater<int> >small;
    int m,n;
    scanf("%d%d",&m,&n);
    for(int i=1;i<=m;i++)
        scanf("%d",&num[i]);
    int cnt=1;
    int op;
    for(int i=1;i<=n;i++)
    {
        cin>>op;
        while(cnt<=op)
        {
            small.push(num[cnt]);
            if(!big.empty()&&small.top()<big.top())//小顶堆里面的数不能比大顶堆里面的数小
            {
                int n1=big.top();
                int n2=small.top();
                big.pop();
                small.pop();
                big.push(n2);
                small.push(n1);
            }
            cnt++;
        }
        printf("%d\n",small.top());
        big.push(small.top());//这句话非常关键。保证了在求第k个最小数时。大顶堆里面保存的是前k-1个最小数
        small.pop();
    }
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115721.html原文链接:https://javaforall.net

(0)
上一篇 2022年1月31日 下午3:00
下一篇 2022年1月31日 下午3:00


相关推荐

  • linux ln 软连接_ln命令建立软链接

    linux ln 软连接_ln命令建立软链接举例:1.对某个文件创建一个连接[root@www.linuxidc.com~]#ln-s/home/kk/ss.sh~#如果不写目标地址,即在当前目录建接立链接[root@www.linuxidc.com~]#lsCentOS-Base.repo.oldboyanaconda-ks.cfginstall.loginstall.log.syslogss.sh…

    2022年9月30日
    4
  • TD—SCDMA_移动TD

    TD—SCDMA_移动TD
    GSM
     globalsystemformobilecommunications全球移动通信系统
     第二代移动电话系统。
         GSM是GlobalSystemofMobilecommunication全球移动通讯系统的英文缩写,是当前应用最为广泛的移动电话标准。全球超过200个国家和地区超过10亿人正在使用GSM电话。所有用户可以在签署了”漫游协定”移动电话运营商之间自由漫游。GSM较之它以前的标准最大的不同是他的信令和语音信道都是数

    2022年10月4日
    5
  • midjourney设计教程

    midjourney设计教程

    2026年3月15日
    2
  • Owasp top10 小结[通俗易懂]

    Owasp top10 小结[通俗易懂]Owasptop101.SQL注入原理:web应用程序对用户输入的数据合法性没有过滤或者是判断,前端传入的参数是攻击者可以控制,并且参数带入数据库的查询,攻击者可以通过恶意的sql语句来实现对数据库的任意操作。2.失效的身份认证和会话管理原理:在开发web应用程序时,开发人员往往只关注Web应用程序所需的功能,所以常常会建立自定义的认证和会话方案。但是要正确的实现这些方案却是很难的。结果就在退出,密码管理,超时,密码找回,账户更新等方面存在漏洞。危害:由于存在以上的漏洞,恶意用户可能会窃取

    2022年5月28日
    80
  • java中定义常量_形参可以是表达式吗

    java中定义常量_形参可以是表达式吗如here所述,javac和其他Java编译器可能为条件为“ConstantExpression”的if语句提供代码消除功能.如果我的代码使用依赖于不同包中定义的其他常量表达式的常量表达式,那么这将如何影响?例如,假设我在相应的指定包中有以下类:packagefoo;publicclassFoo{publicstaticfinalbooleanCONDITION=false;…

    2026年4月18日
    10
  • Vue学习之自定义指令「建议收藏」

    Vue学习之自定义指令「建议收藏」Vue学习之自定义指令

    2022年4月23日
    74

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号