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 }

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

  1. 最大堆 最小堆 解决TOPK问题

    堆:实质是一颗完全二叉树,最大堆的特点:父节点值均大于子节点:最小堆的父节点值均小于子节点: 一般使用连续内存存储堆内的值,因而可以根据当前节点的索引值推断子节点的索引值: 节点i的父节点为(i-1) ...

  2. HDU 1425 sort(堆排序/快排/最大堆/最小堆)

    传送门 Description 给你n个整数,请按从大到小的顺序输出其中前m大的数. Input 每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不 ...

  3. PAT-1147(Heaps)最大堆和最小堆的判断+构建树

    Heaps PAT-1147 #include<iostream> #include<cstring> #include<string> #include<a ...

  4. 502. IPO(最小堆+最大堆法 or 排序法)

    题目: 链接:https://leetcode-cn.com/problems/ipo/submissions/ 假设 力扣(LeetCode)即将开始其 IPO.为了以更高的价格将股票卖给风险投资公 ...

  5. c++/java/python priority_que实现最大堆和最小堆

    #include<iostream>#include<vector>#include<math.h>#include<string>#include&l ...

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

    Google面试题 股市上一个股票的价格从开市开始是不停的变化的,需要开发一个系统,给定一个股票,它能实时显示从开市到当前时间的这个股票的价格的中位数(中值). SOLUTION 1: 1.维持两个h ...

  7. 【数据结构】通用的最小堆(最大堆)D-ary Heap

    听说有一种最小(大)堆,不限于是完全二叉树,而是完全D叉树,名为D-ary Heap(http://en.wikipedia.org/wiki/D-ary_heap).D可以是1,2,3,4,100, ...

  8. 最小堆实现优先队列:Python实现

    最小堆实现优先队列:Python实现 堆是一种数据结构,因为Heapsort而被提出.除了堆排序,“堆”这种数据结构还可以用于优先队列的实现. 堆首先是一个完全二叉树:它除了最底层之外,树的每一层的都 ...

  9. 2018中国大学生程序设计竞赛 - 网络选拔赛 1001 - Buy and Resell 【优先队列维护最小堆+贪心】

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6438 Buy and Resell Time Limit: 2000/1000 MS (Java/O ...

随机推荐

  1. iOS移动开发周报-第18期

    iOS移动开发周报_18期 [摘要]:本期iOS移动开发周报带来如下内容:苹果与 IBM 展开长期深度合作,Swift官方博客,Swift开发的视频教程等. 新闻 <苹果与 IBM 展开长期深度 ...

  2. Selenium学习(二)入门小例子

    1)打开百度页面 2)输入“hello” 3)点击百度一下按钮 from selenium import webdriver url = "http://" + "www ...

  3. Centos7-搭建hdfs启动时报java.net.BindException: Problem binding to [node01:9000] java.net.BindException异常

    今天用阿里的服务器搭了个伪分布式的HDFS,格式化后启动hdfs,发现只有dataNode启动了,查看启动日志发现异常: 2019-01-22 15:54:50,507 FATAL org.apach ...

  4. c# SQLServer导入大批量数据

    说来惭愧,关于批量导入数据,一直采用的是最原始的方式,一条一条插入,或者100条一块批量插入,这种方式,五十步笑百步,并没有明显的性能提升, 昨天在从别的库查询数据到DataTable内存中,然后插入 ...

  5. 2016/7/7 设置wamp2.5 mysql密码 重点是mysql版本

    密码设置时要注意mysql版本.版本不同,效果不同. 方法/步骤     安装好wamp后,右击wamp->MySQl->MySql console(控制台)   提示输入密码,因为密码为 ...

  6. Winform拖拽改变无边框窗体大小

    大家在进行Winform开发过程中,很容易就可以完成一个窗口的布局工作,但现在的软件界面美化效果一个比一个好,很多软件都是无边框的,于是乎,你是不是也感叹:winform的带边框的窗体如此丑陋,我一定 ...

  7. PostgreSQL Client Authentication Configuration File

    PostgreSQL: Documentation: 10: 16.4. Installation Procedure https://www.postgresql.org/docs/10/stati ...

  8. Android笔记之权限库AndPermission

    GitHub地址:https://github.com/yanzhenjie/AndPermission 这个库可以节省不少代码量和时间 使用示例如下 findViewById(R.id.btnGet ...

  9. spawn类参数command详解

    我们主要来看spawn类它的构造方法参数主要有command,从字面上就是指spawn类的子程序用来执行的子程序,也就是系统所能够执行的相应的命令,对于command这个参数,我们是以字符串的方式给出 ...

  10. [coci2012]覆盖字符串 AC自动机

    给出一个长度为N的小写字母串,现在Mirko有M个若干长度为Li字符串.现在Mirko要用这M个字符串去覆盖给出的那个字符串的.覆盖时,必须保证:1.Mirko的字符串不能拆开,旋转:2.Mirko的 ...