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 ...
随机推荐
- Redis学习手册(内存优化)
自从Redis 2.2之后,很多数据类型都可以通过特殊编码的方式来进行存储空间的优化.其中,Hash.List和由Integer组成的Sets都可以通过该方式来优化存储结构,以便占用更少的空间,在有些 ...
- 数据库Mysql性能优化
http://www.cnblogs.com/itxiongwei/p/5533995.html
- map的例子
11.4 编写单词计数程序,忽略大小写和标点.例如,“example.”,“example,"和”Example“应该递增相同的计算器. #include<iostream> # ...
- Linux--------------安装vim
1.相关提示 -bash: vim: command not found 2.查看vim是否安装 rpm -qa|grep vim vim-en ...
- OpenJDK1.8 安装
1. 安装JDK yum install -y java-1.8.0-openjdk-devel 2. 设置环境变量 /etc/profile文件增加 export JAVA_HOME=/usr/li ...
- CodeFile与CodeBehind的区别
引自:http://blog.163.com/wentworth0119@126/blog/static/17321924220122852720103/ asp.net发布项目之后 存在" ...
- 使用的 SQL Server 版本不支持数据类型“datetime2”解决办法
不论下方提示什么数据格式有错误,一般都是entity生成的时候的问题.比如服务器上用的sql2005,自己用的2008. 解决方法: model层生成的model.edmx文件,用记事本打开, 将&l ...
- MySQL的链接,查看数据库,使用数据库,查看表
MySQL的链接,查看数据库,使用数据库,查看表 mysql> show databases; +--------------------+ | Database | +------------ ...
- ionic(一) build你的第一个android apk
1.ionic start myApp tabs >>创建一个app 2.cd myApp >>进入myApp文件 3.ionic platform add andro ...
- MVC小系列(十一)【Html.BeginForm与Ajax.BeginForm】
Html.BeginForm与Ajax.BeginForm都是mvc的表单元素,前者是普通的表单提交,而后者是支持异步的表单提交,直接用mvc自带的Ajax.BeginForm就可以很容易完成一个异步 ...