来源poj1442

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

按上面一行输入,然后输入下面的的次数之后,输出第i个,i是从1开始,输出一次就加1;用两个优先队列,一个从小到大v2,一个从大到小v1,如果输入的数,比v1.top()大就推入2,或者空也推入,否则推入v1,然后把v1.top推入v2;

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define scf(x) scanf("%d",&x)
#define scff(x,y) scanf("%d%d",&x,&y)
#define prf(x) printf("%d\n",x)
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
const ll mod=1e9+7;
const double eps=1e-8;
const int inf=0x3f3f3f3f;
using namespace std;
const double pi=acos(-1.0);
const int N=3e4+5;
priority_queue <ll> v1;//从大到小排
priority_queue <ll,vector<ll>,greater<ll> > v2;
ll num[N];
int main()
{
ll n,m,c=1;
ll ans;
sf("%lld%lld",&n,&m);
rep(i,0,n)
sf("%lld",&num[i]);
int i=0;
while(m--)
{
scf(c);
for(;i<c;i++)
{
if(v1.empty()||v1.top()<num[i])
v2.push(num[i]);
else
{
v1.push(num[i]);
int temp=v1.top();
v2.push(temp);
v1.pop();
}
}
int ans=v2.top();
v2.pop();
v1.push(ans);
prf(ans);
}
return 0;
}

A - Black Box 优先队列的更多相关文章

  1. POJ 1442 Black Box -优先队列

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

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

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

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

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

  4. Black Box《优先队列》

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

  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. 前端工程化系列[04]-Grunt构建工具的使用进阶

    在前端工程化系列[02]-Grunt构建工具的基本使用和前端工程化系列[03]-Grunt构建工具的运转机制这两篇文章中,我们对Grunt以及Grunt插件的使用已经有了初步的认识,并探讨了Grunt ...

  2. IDEA下Maven的Offline Mode

    今天上午别人加了一个Maven依赖项(dependency)我始终获取不到本地 Maven的setting.xml文件检查没有问题 根目录的pom.xml检查也没有问题 本来怀疑公司的Maven服务器 ...

  3. caffe编译时候出现 undefined reference to `TIFFReadRGBAStrip@LIBTIFF_4.0'

    1.编译时候出现 make: * [.build_release/examples/siamese/convert_mnist_siamese_data.bin] Error 1 /usr/local ...

  4. 阿里云配置gitlab邮箱

    gitlab_rails['gitlab_email_from'] = 'username@163.com' user['git_user_email'] = "username@163.c ...

  5. [Python设计模式] 第13章 造小人——建造者模式

    github地址:https://github.com/cheesezh/python_design_patterns 题目1 用程序模拟一个画小人的过程,要求小人要有头,身子,左手,右手,左脚,右脚 ...

  6. HDU 1022.Train Problem I【栈的应用】【8月19】

    Train Problem I Problem Description As the new term comes, the Ignatius Train Station is very busy n ...

  7. iOS ReplayKit实时录制屏幕实现方案的细节记录

    项目有个需求,需要把ios设备上的操作画面实时传输出去,也就是类似推流手机直播画面的方案. 一番调研后发现在ios中,我们可以通过ios自带ReplayKit框架实现. 关于ReplayKit的讲解, ...

  8. linux内核剖析(八)进程间通信之-管道

    管道 管道是一种两个进程间进行单向通信的机制. 因为管道传递数据的单向性,管道又称为半双工管道. 管道的这一特点决定了器使用的局限性.管道是Linux支持的最初Unix IPC形式之一,具有以下特点: ...

  9. csproj文件中copy指令的使用方式

    实际开发中有很多项目需要引用第三方的dll或者资源文件,且文件比较多,在运行时这些文件需要被拷贝到BIN目录. 使用VS自带的"复制到输出目录",似然方便,但是比较不零活,经过多次 ...

  10. ph 提交代码的步骤;

    ph 提交代码的步骤: git status 查看状态: ls -ah 查看文件: git stash list 查看本地缓存的文件: git branch 查看本地的分支: git checkout ...