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 这题的题意:7 是7个数;4是指分4次输入
;第一次·输入1个,并取出第一小的,
第二次输入2个(总共输入两个,要加上第一次输入的不分),取第二小的
第三次输入6个 取第3小的&………… 要是按照输入输出那样一点点的执行,会超时;
用另个优先队列,一个小的在前,一个大的在前,下面我上代码,自己模拟一下看看;
 #include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int main()
{
priority_queue<int,vector<int>,greater<int> >qb;
priority_queue<int,vector<int>,less<int> >qa;
int m,n,a[],b,i,j;
scanf("%d %d",&m,&n);
for(i=; i<m; i++)
scanf("%d",&a[i]);
j=;
for(i=; i<n; i++)
{
scanf("%d",&b);
while(j<b)
qa.push(a[j++]);//压入大的在前的队列
while(qa.size()>i)
{
qb.push(qa.top());
qa.pop();
}
printf("%d\n",qb.top());
qa.push(qb.top());//经过下面两部操作,大的在前的队列中的数不会影响接下来的取值
qb.pop();
}
return ;
}

Black Box《优先队列》的更多相关文章

  1. A - Black Box 优先队列

    来源poj1442 Our Black Box represents a primitive database. It can save an integer array and has a spec ...

  2. POJ 1442 Black Box -优先队列

    优先队列..刚开始用蠢办法,经过一个vector容器中转,这么一来一回这么多趟,肯定超时啊. 超时代码如下: #include <iostream> #include <cstdio ...

  3. POJ 1442 Black Box(优先队列)

    题目地址:POJ 1442 这题是用了两个优先队列,当中一个是较大优先.还有一个是较小优先. 让较大优先的队列保持k个.每次输出较大优先队列的队头. 每次取出一个数之后,都要先进行推断,假设这个数比較 ...

  4. poj 1442 Black Box(优先队列&Treap)

    题目链接:http://poj.org/problem?id=1442 思路分析: <1>维护一个最小堆与最大堆,最大堆中存储最小的K个数,其余存储在最小堆中; <2>使用Tr ...

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

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

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

    Description Our Black Box represents a primitive database. It can save an integer array and has a sp ...

  7. 【优先队列-求第Ki大的数】Black Box

    Black Box Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8637   Accepted: 3542 Descrip ...

  8. 优先队列 || POJ 1442 Black Box

    给n个数,依次按顺序插入,第二行m个数,a[i]=b表示在第b次插入后输出第i小的数 *解法:写两个优先队列,q1里由大到小排,q2由小到大排,保持q2中有i-1个元素,那么第i小的元素就是q2的to ...

  9. Codeforces Round #570 (Div. 3) G. Candy Box (hard version) (贪心,优先队列)

    题意:你有\(n\)个礼物,礼物有自己的种类,你想将它们按种类打包送人,但是打包的礼物数量必须不同(数量,与种类无关),同时,有些礼物你想自己留着,\(0\)表示你不想送人,问你在送出的礼物数量最大的 ...

随机推荐

  1. 无需Cygwin,如果没有在命令行,Eclipse编NDK

    此链接    http://blog.csdn.net/xiaodongrush/article/details/28908829 參考链接    http://www.cnblogs.com/che ...

  2. iOS 高仿:花田小憩3.0.1

    前言 断断续续的已经学习Swift一年多了, 从1.2到现在的2.2, 一直在语法之间徘徊, 学一段时间, 工作一忙, 再捡起来隔段时间又忘了.思来想去, 趁着这两个月加班不是特别多, 就决定用swi ...

  3. href与src的区别

    src是source的缩写,指向外部资源的位置,指向的内容将会嵌入到文档中当前标签所在位置:在请求src资源时会将其指向的资源下载并应用到文档内,例如js脚本,img图片和frame等元素. href ...

  4. React组件的生命周期各环节运作流程

    'use strict'; React.createClass({ //1.创建阶段 getDefaultProps:function(){ //在创建类的时候被调用 console.log('get ...

  5. gulp的常用api

    gulp是什么? http://gulpjs.com/ 相信你会明白的! 与著名的构建工具grunt相比,有什么优势呢? 易于使用,代码优于配置 高效,不会产生过多的中间文件,减少I/O压力 易于学习 ...

  6. Weex 样式

    1.盒型 width height padding padding-left padding-right padding-top padding-bottom margin margin-left m ...

  7. DIV布局之道三:DIV块的覆盖,DIV层遮盖其他DIV

    DIV布局网页的第三种方式:覆盖.DIV覆盖方式经常应用于网页弹出框的制作,例如在网店系统中,当用户没有登录时,点击购买,系统弹出一个登陆框. 请看代码: HTML部分: XML/HTML Code复 ...

  8. sql-从查询结果创建一个永久表

    语法: select x into new_tableName  from ori_tableName 例如: SELECT [site] ,[day] ,[val]/31.4 [val] into ...

  9. linq学习笔记:将List<T> 转换为 Dictionary<T Key,T Value>

    运用Linq,将List<T> 转换为 Dictionary<T Key,T Value> 即:List<T>  ToDictionary<T Key,T V ...

  10. Linux 自动更新时间

    1. 从NTP上把时间同步到本地 cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 2. 更新本地时间 ntpdate us.pool.ntp.o ...