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 ...
随机推荐
- C++ Combobox输入时自动完成
Combobox 在输入时自动完成 关键点 实现过程 BOOL m_bAuto; BOOL CProject02Dlg::PreTranslateMessage(MSG* pMsg) { i ...
- [Javascript] Get Started with LeafletJS Mapping
Leaflet makes creating maps in the browser dead simple. With some HTML and 3 lines of JavaScript, we ...
- jQuery源代码学习笔记:构造jQuery对象
2.1源代码结构: (function( window, undefined ) { var jQuery = (function() { // 构建jQuery对象 var jQuery = fun ...
- 一个卡片式的ViewPager,带你玩转ViewPager的PageTransformer属性!
ViewPager的基本用法不必多说,这都很简单,我们可以在ViewPager中加载一个ImageView,也可以加载一个Fragment,这都是目前非常常见的用法.那么我今天说的是ViewPager ...
- byte数组与对象之间的相互转换
在进行网络通信时可能需要传输对象,如果用NIO的话,只能用Bytebuffer和channel直接 通过ByteArray*Stream和Object*Stream可以将byte数组和对象进行相互的转 ...
- 获取本地IP和端口号的指令
ipconfig就可以获取ip 获取端口号的指令: 开始--运行--cmd--输入netstat an(中间有一空格)
- Clean Code(三):注释
在工作中经常在刚开始写代码的时候,写下类与方法的注释,时间久了,CR多了,也不可能一直去修改注释了.加班都累成dog.注释才不改呢,就是这么任性,哈哈. 项目久了,注释会欺骗阅读者,本人被骗过,也骗过 ...
- Python模拟键盘输入和鼠标操作
Python模拟键盘输入和鼠标操作 一.Python键盘输入模拟: import win32api import win32con win32api.keybd_event(17,0,0,0) #c ...
- java_method_删除事务回滚
public String[] deleteEPGroup(String groupID, String groupName) { String[] operRes=new String[3]; if ...
- C#里面比较时间大小三种方法
1.比较时间大小的实验 string st1="12:13";string st2="14:14";DateTime dt1=Convert.ToDateTim ...