Black Box《优先队列》
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 这题的题意: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《优先队列》的更多相关文章
- A - Black Box 优先队列
来源poj1442 Our Black Box represents a primitive database. It can save an integer array and has a spec ...
- POJ 1442 Black Box -优先队列
优先队列..刚开始用蠢办法,经过一个vector容器中转,这么一来一回这么多趟,肯定超时啊. 超时代码如下: #include <iostream> #include <cstdio ...
- POJ 1442 Black Box(优先队列)
题目地址:POJ 1442 这题是用了两个优先队列,当中一个是较大优先.还有一个是较小优先. 让较大优先的队列保持k个.每次输出较大优先队列的队头. 每次取出一个数之后,都要先进行推断,假设这个数比較 ...
- poj 1442 Black Box(优先队列&Treap)
题目链接:http://poj.org/problem?id=1442 思路分析: <1>维护一个最小堆与最大堆,最大堆中存储最小的K个数,其余存储在最小堆中; <2>使用Tr ...
- [ACM] POJ 1442 Black Box (堆,优先队列)
Black Box Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7099 Accepted: 2888 Descrip ...
- Black Box--[优先队列 、最大堆最小堆的应用]
Description Our Black Box represents a primitive database. It can save an integer array and has a sp ...
- 【优先队列-求第Ki大的数】Black Box
Black Box Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8637 Accepted: 3542 Descrip ...
- 优先队列 || POJ 1442 Black Box
给n个数,依次按顺序插入,第二行m个数,a[i]=b表示在第b次插入后输出第i小的数 *解法:写两个优先队列,q1里由大到小排,q2由小到大排,保持q2中有i-1个元素,那么第i小的元素就是q2的to ...
- Codeforces Round #570 (Div. 3) G. Candy Box (hard version) (贪心,优先队列)
题意:你有\(n\)个礼物,礼物有自己的种类,你想将它们按种类打包送人,但是打包的礼物数量必须不同(数量,与种类无关),同时,有些礼物你想自己留着,\(0\)表示你不想送人,问你在送出的礼物数量最大的 ...
随机推荐
- 还原virtual函数的本质-----C++
当你每次看到C++类中声明一个virtual函数,特别是看到了一个virtual的虚构函数.你知道它的意思吗?你肯定会毫不犹豫的回答:不就是多态么...在运行时确定具体的行为么...完全正确,但这里我 ...
- tcpdump抓包以及port查看的一些操作
1.tcpdump. nginx开启后会占用80端口,此时运行命令:tcpdump tcp port 80 结果例如以下: [syswj@host ~]$ sudo tcpdump tcp port ...
- [转] nginx 开启gzip压缩--字符串压缩比率很牛叉
http://www.cnblogs.com/dasn/articles/3716055.html 刚刚给博客加了一个500px相册插件,lightbox引入了很多js文件和css文件,页面一下子看起 ...
- [转] Scale-up 和Scale-out的区别
http://www.cnblogs.com/spork/archive/2009/12/29/1634766.html 来自原小站,曾经迷糊过的东西,表上来,希望对正在迷糊或即将迷糊的人有帮助. 谈 ...
- unity介绍
- jad批量反编译class和jadeclipse集成到eclipse的设置方法
安装jad配置 1.从http://varaneckas.com/jad/下载windows版本的jad.exe 2.安装完毕后配置jad的系统环境变量 批量解压jar和class文件 1.使用7zi ...
- css兼容性问题
其实做网页最大的问题还是兼容性吧,要调试IE的各种浏览器. DIV+CSS设计IE6.IE7.FF 兼容性 DIV+CSS网页布局这是一种趋势,我也开始顺应这股趋势了,不过在使用DIV+CSS网站设 ...
- Java使用poi对Execl简单_读_操作
public class ReadExecl { // private final String XLSX = ".xlsx"; // 2007以上版本 // private fi ...
- apache commons io包基本功能
1. http://jackyrong.iteye.com/blog/2153812 2. http://www.javacodegeeks.com/2014/10/apache-commons-io ...
- angularjs-ngModel传值问题
js NiDialog.open({ windowClass: '', backdrop: 'static', keyboard: false, templateUrl: '/static/tpl/a ...