[ACM] POJ 1442 Black Box (堆,优先队列)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 7099 | Accepted: 2888 |
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
Source
解题思路:
题意不好懂。。
我直接解释下例子什么意思把。
7 4
3 1 -4 2 8 -1000 2
1 2 6 6
7代表以下给定7个数的数字序列。4能够理解为四次查询, 1 2 6 6为查讯。第一次查询是求数字序列仅仅有前一个数(3)时。此时的第一小的数字,即1,第二次查询是求数字序列仅仅有前2个数(3 1)时。此时的第二小的数字,即 3,第三次查询是求数字序列仅仅有前6个数时(3,1,-4,2,8,-1000)。此时的第三小的数字,即1,第四次查询是求数字序列仅仅有前6个数时。此时的第四小的数字。
思路为: 当求第4小的数字时,我们用一个大顶堆来维护前三个最小的数字,那么小顶堆的堆顶即为所求。
用priority_queue<int>big; 优先队列还起到大顶堆的作用,顶部即为最大值 ,即 big.top();
priority_queue<int,vector<int>,greater<int> >small; 小顶堆。顶堆为最小值 。即 small.top();
用例子来说明一下 大顶堆和小顶堆工作方法:
1 2 6 6
首先是1: 把第一个数3 插入到小顶堆中,这时候推断。假设大顶堆不为空且小顶堆的top小于大顶堆的top时,就把二者的top值互换,由于,大顶堆中的数不能比小顶堆中的大。大顶堆维护第i次查询时,前i-1个最小的数,这时候大顶堆为空,不用互换值。 输出第一次查询时前1个数的第1小的数即为 小顶堆的top 3,然后把小顶堆的top 移除。放到大顶堆中。
然后是2: 把第二个数1插入到小顶堆中。推断,大顶堆不为空,小顶.top() 1 <大顶.top() 3 ,所以二者互换,小顶.top()为3,大顶top()为1。 然后输出小顶.top(),即为第二小的数。把小顶的.top()移除,放入大顶中。这是大顶中维护的是眼下数字中前两个最小的数。
然后是6: 要求前6个数中第3小的数字,先得把3 1以后的四个数字插入才可以六个数。依次插入, -4插入小顶堆。这时 大顶堆 3 1,-4<3,互换, 小顶堆 3,大顶堆为1 -4 ,2插入小顶堆,小顶堆 2,3 大顶堆 1 -4, 2>1,不用互换, 8插入到小顶堆。小顶堆为 2,3,8 大顶堆为 1。-4。 2>1,不用互换,-1000插入到小顶堆。小顶堆 -1000,2,3,8,大顶堆1,-4, -1000<1,不行,互换以后得小顶堆。1,2,3,8 , 大顶堆 -1000。-4
,输出小顶堆.top() 1。 并把它移除放入到大顶堆中。为下次查询做准备。
所以从以上能够看出,关键点就是第i次查询时,大顶堆中维护的总是眼下数字中最小的 i-1个数。
代码:
#include <iostream>
#include <stdio.h>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn=30010;
int num[maxn];
int n,m; int main()
{
priority_queue<int>big;
priority_queue<int,vector<int>,greater<int> >small;
int m,n;
scanf("%d%d",&m,&n);
for(int i=1;i<=m;i++)
scanf("%d",&num[i]);
int cnt=1;
int op;
for(int i=1;i<=n;i++)
{
cin>>op;
while(cnt<=op)
{
small.push(num[cnt]);
if(!big.empty()&&small.top()<big.top())//小顶堆里面的数不能比大顶堆里面的数小
{
int n1=big.top();
int n2=small.top();
big.pop();
small.pop();
big.push(n2);
small.push(n1);
}
cnt++;
}
printf("%d\n",small.top());
big.push(small.top());//这句话非常关键。保证了在求第k个最小数时。大顶堆里面保存的是前k-1个最小数
small.pop();
}
return 0;
}
[ACM] POJ 1442 Black Box (堆,优先队列)的更多相关文章
- POJ 1442 Black Box 堆
题目: http://poj.org/problem?id=1442 开始用二叉排序树写的,TLE了,改成优先队列,过了.. 两个版本都贴一下吧,赚稿费.. #include <stdio.h& ...
- 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(优先队列&Treap)
题目链接:http://poj.org/problem?id=1442 思路分析: <1>维护一个最小堆与最大堆,最大堆中存储最小的K个数,其余存储在最小堆中; <2>使用Tr ...
- POJ 1442 Black Box -优先队列
优先队列..刚开始用蠢办法,经过一个vector容器中转,这么一来一回这么多趟,肯定超时啊. 超时代码如下: #include <iostream> #include <cstdio ...
- 数据结构(堆):POJ 1442 Black Box
Black Box Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10658 Accepted: 4390 Descri ...
- 优先队列 || POJ 1442 Black Box
给n个数,依次按顺序插入,第二行m个数,a[i]=b表示在第b次插入后输出第i小的数 *解法:写两个优先队列,q1里由大到小排,q2由小到大排,保持q2中有i-1个元素,那么第i小的元素就是q2的to ...
- Running Median POJ - 3784 (对顶堆/优先队列 | 链表)
For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After ...
随机推荐
- C++ 文本读写
写文件: ofstream of; of.open("test.txt"); string content = "abcd"; of.write(content ...
- 静默安装ORACLE【weber出品必属精品】
安装配置系统环境安装linux ,所有服务都不选择,只是选择安装开发工具,不要安装防火墙(当然也可以在后面关闭) 打开终端,执行如下命令,检查安装包,没有的都要安装 make, glibc, liba ...
- 武汉科技大学ACM:1008: 明明的随机数
Problem Description 明明想在学校中请一些同学一起做一项问卷 调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤100),对于其中重复的数字,只保留一个, ...
- Ehcache入门(一)——开发环境的搭建
EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. 那么.如何搭建Ehcache的开发环境呢? 1.下载相关的jar包,这 ...
- zabbix 通过smtp 邮件报警
注:sendemail 不是sendmail....sendemail是用perl语言写的一个smtp发邮件的小程序....详情可自行查阅..... 1. media 用户配置下的media. Adm ...
- nodejs调试
1.通过debug命令进行调试 node debug app.js 运行的结果: 在debug状态下输入"repl"命令可以评估变量和表达式的值 按下'CTRL+C'可以退出rep ...
- jQuery版本引发的血案 iframe error 和 checkbox 无法勾选
问题介绍: 1.由于我们的项目里面用了很多Iframe,在初始话加载的时候页面就会报错.一开始调试很久没找到什么原因,看打印结果页面会被两次load,只能一步步找, 最后发现在document rea ...
- 在PHP中获取日期和时间
PHP提供了多种获取时间和日期的函数,除了通过time()函数获取当前的UNIX时间戳外,调用getdate()函数确定当前时间,通过gettimeofday()函数获取某一天中的具体时间.此外,在P ...
- php的setcookie()函数详解
一.浏览器COOKIE原理: 浏览器在访问某个域名时会先读取本地的COOKIE文件(CHROME浏览器在C:\Users\Administrator\AppData\Local\Google\Chro ...
- mysql 获取当前日期及格式化
MYSQL 获取当前日期及日期格式获取系统日期: NOW()格式化日期: DATE_FORMAT(date, format)注: date:时间字段format:日期格式 返回系统日期,输出 2009 ...