数据结构(堆):POJ 1442 Black Box
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 10658 | Accepted: 4390 |
Description
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
Output
Sample Input
7 4
3 1 -4 2 8 -1000 2
1 2 6 6
Sample Output
3
3
1
2
水题瞬秒……
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int maxn=;
int a[maxn],t[maxn],n,m;
priority_queue<int>A;
priority_queue<int,vector<int>,greater<int> >B;
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)scanf("%d",&a[i]);
for(int i=;i<=m;i++)scanf("%d",&t[i]);
for(int i=,p=;i<=m;i++){
while(p!=t[i])B.push(a[++p]);
while(A.size()<1ul*i){
A.push(B.top());
B.pop();
}
while(B.size()&&A.top()>B.top()){
A.push(B.top());
B.push(A.top());
A.pop();B.pop();
}
printf("%d\n",A.top());
}
return ;
}
数据结构(堆):POJ 1442 Black Box的更多相关文章
- POJ 1442 Black Box treap求区间第k大
题目来源:POJ 1442 Black Box 题意:输入xi 输出前xi个数的第i大的数 思路:试了下自己的treap模版 #include <cstdio> #include < ...
- POJ 1442 Black Box(优先队列)
题目地址:POJ 1442 这题是用了两个优先队列,当中一个是较大优先.还有一个是较小优先. 让较大优先的队列保持k个.每次输出较大优先队列的队头. 每次取出一个数之后,都要先进行推断,假设这个数比較 ...
- poj 1442 Black Box(堆 优先队列)
题目:http://poj.org/problem?id=1442 题意:n,m,分别是a数组,u数组的个数,u[i]w为几,就加到a几,然后输出第i 小的 刚开始用了一个小顶堆,超时,后来看了看别人 ...
- POJ 1442 Black Box 堆
题目: http://poj.org/problem?id=1442 开始用二叉排序树写的,TLE了,改成优先队列,过了.. 两个版本都贴一下吧,赚稿费.. #include <stdio.h& ...
- [ACM] POJ 1442 Black Box (堆,优先队列)
Black Box Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7099 Accepted: 2888 Descrip ...
- poj 1442 Black Box(优先队列&Treap)
题目链接:http://poj.org/problem?id=1442 思路分析: <1>维护一个最小堆与最大堆,最大堆中存储最小的K个数,其余存储在最小堆中; <2>使用Tr ...
- POJ 1442 Black Box
第k大数维护,我推荐Treap..谁用谁知道.... Black Box Time ...
- POJ 1442 Black Box -优先队列
优先队列..刚开始用蠢办法,经过一个vector容器中转,这么一来一回这么多趟,肯定超时啊. 超时代码如下: #include <iostream> #include <cstdio ...
- 优先队列 || POJ 1442 Black Box
给n个数,依次按顺序插入,第二行m个数,a[i]=b表示在第b次插入后输出第i小的数 *解法:写两个优先队列,q1里由大到小排,q2由小到大排,保持q2中有i-1个元素,那么第i小的元素就是q2的to ...
随机推荐
- oracle左右连接 完全连接 有效连接 心得总结
左链接 A表 Left join B表 on 条件 示例 A表 B表 SELECT * FROM A left JOIN B ON A.AID = B.BID; 结果: 左链接查询出来的数 ...
- 基于Html5的兼容所有主流浏览器的在线视频播放器videoJs
在一个新的项目上需要实现在线视频播放,原本打算借助优酷的视频存储和播放,但是发现这个需要用户注册优酷账户,严重影响用户体验,于是这个方案被毙掉了.于是开始了自己开发一个在线播放器的想法,当然尽量使用已 ...
- post get
/// <summary> /// Get方法 /// </summary> /// <param name="serverUrl">url地址 ...
- C#中Dictionary、ArrayList、Hashtable和Array的区别
IDictionary接口是所有字典类集合的基本接口,该接口与ICollection,IEnumerable接口是所有非泛型类集合的最基本的接口 IEnumerable接口用于公开枚举数,该枚举数支持 ...
- 为Angular-UEditor增加工具栏属性
感谢胡大大分享的的开源项目 Angular 的 UEditor 插件 Angular-UEditor 本文只是修改了angular-ueditor.js,加入了对工具栏的定制,方便项目使用 1 (fu ...
- csv文本编辑引号问题
今天发现一个csv的一个问题,csv工具类对于引号默认有特殊的处理.我希望写出来的结果是 1,"1",1 原来的代码是 CsvWriter cw=new CsvWriter(&qu ...
- 完美让IE兼容input placeholder属性的jquery实现
调用时直接引用jquery与下面的js就行了,相对网上的大多数例子来说,这个是比较完美的方案. /* * 球到西山沟 * http://www.cnzj5u.com * 2014/11/26 12:1 ...
- TestNG目录
1 - 简介 2 - 注解 3 - testng.xml 4 - 执行 TestNG 5 - 测试方法, 测试类 和 测试组 5.1 - 测试方法 5.2 - 测试组 5.3 ...
- c++ undefined reference to mysqlinit
Solved g++ $(mysql_config --cflags) file.cpp -o filename $(mysql_config --libs)
- C#快速导入海量XML数据至SQL Server数据库
#region 将Xml中的数据读到Dataset中,然后用SqlBulkCopy类把数据copy到目的表中using (XmlTextReader xmlReader = new XmlTextRe ...