来源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. Uva11582 Colossal Fibonacci Numbers!(同余模定理+快速幂)

    https://vjudge.net/problem/UVA-11582 首先明确,斐波那契数列在模c的前提下是有循环节的.而f[i] = f[i-1]+f[i-2](i>=2)所以只要有两个连 ...

  2. /debug/requests is already registered. You may have two independent copies of golang.org/x/net/trace in your binary, trying to maintain separate state. This may involve a vendored copy of golang.org/x

    找到问题就很好解决了,直接百度  go依赖管理-govendor go get -u github.com/kardianos/govendor 先获取这个,然后将govendor.exe放入path ...

  3. MYSQL的联合查询最好是少用,效能差异巨大

    同样的功能,不同的写法,时间和内存占用差了几千倍,不废话,直接上代码 第一种写法: 代码如下: $Rs=DB::get($_ENV['DB'],3,"SELECT * FROM _xiazh ...

  4. Docker Compose安装以及入门

    Docker Compose 是 Docker 官方编排(Orchestration)项目之一,负责快速在集群中部署分布式应用. Compose 简介 Compose 项目是 Docker 官方的开源 ...

  5. IDEA修改JDK(全)

    https://www.cnblogs.com/hkgov/p/8074085.html 解决:javac: 无效的目标发行版: 1.8 解决:项目JDK版本不对 解决:Jar包问题 1," ...

  6. SpringBoot2.0+Shiro+JWT 整合

    SpringBoot2.0+Shiro+JWT 整合 JSON Web Token(JWT)是一个非常轻巧的规范.这个规范允许我们使用 JWT 在用户和服务器之间传递安全可靠的信息. 我们利用一定的编 ...

  7. PowerShell 显示气球提示框 1

    #加载 Winform 程序集,使用Out-Null抑制输出 [system.Reflection.Assembly]::LoadWithPartialName('System.Windows.For ...

  8. SQLServer Always On FCI 脑裂及可疑状态修复

    FCI 双节点集群,因为晚上集群节点间的网络中断过.两个节点都觉得还有一个节点宕机,在各节点的集群管理中都看到对方已宕机. 连接到集群IP.提示 msdb 数据库有问题: watermark/2/te ...

  9. Python并行实例

    任务 def single(): # 单进程单线程实现 s = 0 for i in range(1, N): s += math.sqrt(i) return s 结论 Python多线程无法利用多 ...

  10. 21.翻译系列:Entity Framework 6 Power Tools【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/entity-framework-power-tools.aspx 大家好,这里就是EF ...