POJ 1442 Black Box 堆
题目: http://poj.org/problem?id=1442
开始用二叉排序树写的,TLE了,改成优先队列,过了。。
两个版本都贴一下吧,赚稿费。。
#include <stdio.h>
#include <queue>
#include <vector>
using namespace std;
priority_queue<int>qmax;
priority_queue<int, vector<int>, greater<int> >qmin; int add[]; int main()
{
int n, m, last = , get;
scanf("%d %d", &n, &m);
for(int i = ; i < n; i++)
scanf("%d", &add[i]);
for(int i = ; i <= m; i++)
{
scanf("%d", &get);
while(last < get)
qmax.push(add[last++]);
while(qmax.size() >= i)
{
qmin.push(qmax.top());
qmax.pop();
}
qmax.push(qmin.top());
qmin.pop();
printf("%d\n", qmax.top());
}
return ;
}
优先队列AC
#include <stdio.h>
#include <stdlib.h> struct node
{
int data;
struct node *left, *right;
};
int add[], cnt; void build(struct node *&p, int k)
{
if(p == NULL)
{
p = (struct node *)malloc(sizeof(struct node));
p->data = k;
p->left = p->right = NULL;
return;
}
if(p->data >= k)
build(p->left, k);
else build(p->right, k);
} void seek(struct node *p, int x)
{
if(p == NULL || cnt > x)return;
seek(p->left, x);
if(cnt++ == x)
printf("%d\n", p->data);
seek(p->right, x);
} int main()
{
int n, m, last = , get;
scanf("%d %d", &n, &m);
struct node *root = NULL;
for(int i = ; i < n; i++)
scanf("%d", &add[i]);
for(int i = ; i <= m; i++)
{
scanf("%d", &get);
while(last < get)
build(root, add[last++]);
cnt = ;
seek(root, i);
}
return ;
}
二叉排序树TLE
POJ 1442 Black Box 堆的更多相关文章
- [ACM] POJ 1442 Black Box (堆,优先队列)
Black Box Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7099 Accepted: 2888 Descrip ...
- 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
Black Box Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10658 Accepted: 4390 Descri ...
- 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 ...
随机推荐
- 跟我一起学extjs5(05--主界面上增加顶部和底部区域)
跟我一起学extjs5(05--主界面上增加顶部和底部区域) 这一节为主界面加一个顶部区域和底部区域. 一个管理系统的界面能够粗分为顶部标题部分.中间数据展示和处理的部分.底部备注和状 ...
- Kinect开发学习笔记之(一)Kinect介绍和应用
Kinect开发学习笔记之(一)Kinect介绍和应用 zouxy09@qq.com http://blog.csdn.net/zouxy09 一.Kinect简单介绍 Kinectfor Xbox ...
- MyEclipse的快捷键的使用
MyEclipse的10个快捷键:Ctrl + Shift + T: 打开类型:显示"打开类型"对话框来在编辑器中打开类型."打开类型"选择对话框显示工作空间中 ...
- cglib源码分析(三):Class生成策略
cglib中生成类的工作是由AbstractClassGenerator的create方法使用相应的生成策略完成,具体代码如下: private GeneratorStrategy strategy ...
- Java实现折半(二分)插入排序
/*折半插入查找思想:每趟将一个带排序的元素作为关键字插入到已经排好的部分序列的适当位置上,查找适当位置的方法用折半查找法 * 适合记录数较多的场景 * 在查找插入位置时节省了时间 * 在记录移动次数 ...
- android中实现简单的播放
MediaPlayer mediaPlayer1; mediaPlayer1 = MediaPlayer.create(getBaseContext(), R.raw.ic_yanyuan); med ...
- 安卓开发中使用Genymotion模拟器
在安卓开发中,运行和调试自己所写的安卓程序需要用到模拟器 在一般情况下 是直接在这创建一个模拟器,但是这种自带的模拟器运行效率不佳,而且启动时间漫长 所以,我们可以换一款安卓模拟器 Genymotio ...
- Spring 3.0 + Atomikos构建jta分布式事务
Spring3.0已经不再支持jtom了,不过我们可以用第三方开源软件atomikos(http://www.atomikos.com/)来实现.Atomikos是目前在分布式事务管理中做得相当不错的 ...
- DES加密系统的实现
这是一个高内聚低耦合可复用的DES加密系统的实现. Github 链接:https://github.com/cyendra/CyDES 要实现加密系统,先考虑数据的基本单位. 在DES加密中,数据是 ...
- CentOs6.8安装Git并安装oh my zsh
(一)git安装 1.下载git2.4.9或其他版本 Index of /pub/software/scm/git git各个版本下载链接: https://www.kernel.org/pub/so ...