Black Box--[优先队列 、最大堆最小堆的应用]

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

解题思路分析:拿到这道题最先想到的思路是(错误):  1.用队列保存add命令,用vector保存进入黑箱的元素。  2.每次向黑箱加入元素后,对黑箱中元素排序,输出第i个元素。这个算法看似没用问题,但实际上,经过极端数据测试,在M、N均为30000时,跑完程序需要2.7s,会超时,原因是每一次添加完元素后都要对所有元素排序,时间开销太大了。然而经过分析容易发现,实际上并没有必要每次都对所有元素排序,我们只需要保存前i-1小的元素,然后找出第i个元素起之后的最小元素与前i-1个元素中最大的元素比较,即可得到第i小的元素。这样思路就明了了,下面给出正确思路:  1.用优先队列分别建立最小堆(顶部最小)和最大堆(顶部最大);  2.每次i增加时,将最小堆顶部元素加入最大堆;  3.每次加入元素时比较最小堆顶部元素和最大堆顶部元素,如果“top小”>“top大”,则交换两个堆顶部元素;  4.输出最小堆顶部元素。注意优先队列的两种用法:

 1. 标准库默认使用元素类型的<操作符来确定它们之间的优先级关系。

priority_queue<int> q;

 通过<操作符可知在整数中元素大的优先级高。
  2. 数据越小,优先级越高  greater<int> 定义在头文件 <functional>中

priority_queue<int, vector<int>, greater<int> >q; 
3.自定义比较运算符<

代码如下:
 1 #include <iostream>
 2 #include <set>
 3 #include <vector>
 4 #include <cstdio>
 5 #include <queue>
 6 #include <algorithm>
 7 #include <time.h>
 8 #include <functional>
 9 using namespace std;
10 #define clock__ (double(clock())/CLOCKS_PER_SEC)
11
12 int M,N;
13 queue<int> A;//存储add命令
14 priority_queue<int,vector<int>,greater<int> > A_min;//最小堆
15 priority_queue<int> A_max;//最大堆
16 int i;
17
18 int main() {
19
20     i=0;
21     scanf("%d%d",&M,&N);
22     while (M--) {
23         int a;
24         scanf("%d",&a);
25         A.push(a);
26     }
27     while(N--){
28         int u;
29         scanf("%d",&u);
30         i++;
31         if(A_min.size()!=0) {
32             A_max.push(A_min.top());
33             A_min.pop();
34         }
35         int n=u-(A_min.size()+A_max.size());
36         while(n--){
37             A_min.push(A.front());A.pop();
38             if(A_max.size()&&A_min.size()&&A_min.top()<A_max.top()){
39                 int a=A_max.top(); A_max.pop();
40                 int b=A_min.top(); A_min.pop();
41                 A_min.push(a);A_max.push(b);
42             }
43         }
44         printf("%d\n",A_min.top());
45
46     }
47    // cout<<"time : "<<clock__<<endl;
48     return 0;
49 }

  
时间: 2024-08-25 07:20:44

Black Box--[优先队列 、最大堆最小堆的应用]的相关文章

最大堆/最小堆/优先队列 实现代码(c++)

自我感觉代码写的比较乱,这方面要好好注意一下. 总结: 1.在使用vector<int>::size_type 类似的类型时,千万要注意循环的条件判断,很容易发生溢出的危险!所以我最后很懒的选择使用int  - -. 2.下标表示和元素个数表示之间的细微差别.下标之间的变换关系: 父节点 parent(i)=(i-1)/2; 左孩子 left(i)=2*i+1;右孩子 right(i)=2*i+2 class Max_Heap{ typedef int index; public: Max_H

优先队列及最小堆最大堆

为什么优先队列里默认是堆(heap)实现,默认是优先级高的出队,定义结构体重载函数为什么要按照从小到大排序?原来是自己对优先队列还不太了解: 1 堆 1.1 简介 n个关键字序列Kl,K2,-,Kn称为(Heap),当且仅当该序列满足如下性质(简称为堆性质): (1)ki<=k(2i)且ki<=k(2i+1)(1≤i≤ n),当然,这是小根堆,大根堆则换成>=号.//k(i)相当于二叉树的非叶结点,K(2i)则是左孩子,k(2i+1)是右孩子 若将此序列所存储的向量R[1..n]看做是一

最小堆(优先队列)基本概念,即一个完整建立,插入,删除代码

堆(优先队列)priority queue特殊的队列,取出元素的顺序是依照元素的优先权(关键字)大小,而出元素进入队列的先后顺序操作:查找最大值(最小值),删除(最大值) 数组:链表:有序数组:有序链表: 采用二叉搜索树? NO 采用完全二叉树 YES堆的连个特性结构性:用数组表示的完全二叉树:有序性:任一结点的关键字是其字树所有结点的最大值(或最小值) 最大堆(MaxHeap)也称大顶堆:最大值 最小堆(MinHeap)也称"小顶堆":最小值 从根节点到任意结点路径上结点序列的有序性

数据结构-最大堆、最小堆【手动实现】

0,堆的简介 数据结构中的堆是一种特殊的二叉树,不同于 Java 内存模型中的堆. 堆必须符合以下两个条件: 是一棵完全二叉树. 任意一个节点的值都大于(或小于)左右子节点的值. 从第一点可以知道,堆适合用数组来存储. 第二点中,若父节点都大于等于左右子节点,则被称为大顶堆,反之则为小顶堆. 图-最大堆及其存储方式 0.1节点的父.子节点关系 一个节点[根节点除外]的父节点地址为其地址的二分之一,它的左子节点地址为其地址值的2倍,右子节点地址为其地址2倍加1.  例如:现在有个节点的地址为3,其

通用的最小堆(最大堆)D-ary Heap

听说有一种最小(大)堆,不限于是完全二叉树,而是完全D叉树,名为D-ary Heap(http://en.wikipedia.org/wiki/D-ary_heap).D可以是1,2,3,4,100,对于优先队列该有的功能都没有问题. 动手写一个D-ary Heap,应该不难.简单起见,不考虑像STL一样通过template传入Comp类,下面的实现要求T类型重载了operator <和operator >. template<class T> class DaryHeap { s

最小堆_最大堆

在大数查找中会遇到一类问题,例如在100亿条数据中找出 最大的(最小的) 前1000个元素.以int型4Byte为例,有1*1010*4 B = 4*1010/(230) B = 37.25G. 直接读取到内存中显然不合适,那么就需要: 首先,读取前1000个元素,建立一个最小堆(最大堆): 其次,之后每读取一个元素就与最小堆根元素(1000个数中最小值)进行比较: 如果,新元素大于(小于)堆顶元素 则,删除堆顶元素,将新元素插入堆顶.然后调整堆序,删除堆顶....循环往复 #define le

Google 面试题:Java实现用最大堆和最小堆查找中位数 Find median with min heap and max heap in Java

Google面试题 股市上一个股票的价格从开市开始是不停的变化的,需要开发一个系统,给定一个股票,它能实时显示从开市到当前时间的这个股票的价格的中位数(中值). SOLUTION 1: 1.维持两个heap,一个是最小堆,一个是最大堆. 2.一直使maxHeap的size大于minHeap. 3. 当两边size相同时,比较新插入的value,如果它大于minHeap的最大值,把它插入到minHeap.并且把minHeap的最小值移动到maxHeap. ...具体看代码 1 /*********

hdu 4006 The kth great number (优先队列+STB+最小堆)

The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 6637    Accepted Submission(s): 2671 Problem Description Xiao Ming and Xiao Bao are playing a simple Numbers game. In a roun

C++ priority_queue 最大堆、最小堆

问题描述 通常在刷题的时候,会遇到最大堆.最小堆的问题,这个时候如果自己去实现一个也是OK的,但是通常时间不太够,那么如何处理?这时,就可以借助C++ STL的priority_queue. 具体分析 需要注意的是,C++ STL默认的priority_queue是将优先级最大的放在队列最前面,也即是最大堆.那么如何实现最小堆呢? 假设有如下一个struct: struct Node { int value; int idx; Node (int v, int i): value(v), idx