题目链接:http://poj.org/problem?id=1442

思路分析:

<1>维护一个最小堆与最大堆,最大堆中存储最小的K个数,其余存储在最小堆中;

<2>使用Treap构造名次树,查询第K大数即可;

代码如下(堆的用法):

#include<iostream>
#include<queue>
using namespace std; struct cmp1
{
bool operator() ( const int a, const int b )
{ return a > b; }
}; struct cmp2
{
bool operator()( const int a, const int b )
{ return a < b; }
}; priority_queue<int,vector<int>,cmp1>MaxHeap;
priority_queue<int,vector<int>,cmp2>MinHeap;
int a[]; int main()
{
int m, n, i, K;
int Count = , Tmp; scanf( "%d%d", &m, &n );
for ( i = ; i < m; i++ )
scanf( "%d", &a[i] ); for( i = ; i < n; i++ )
{
scanf( "%d", &K );
while ( Count < K )
{
MaxHeap.push(a[Count]);
if( !MinHeap.empty() && MaxHeap.top() < MinHeap.top() )
{
Tmp = MaxHeap.top();
MaxHeap.pop();
MaxHeap.push( MinHeap.top() );
MinHeap.pop();
MinHeap.push(Tmp);
}
Count++;
}
printf( "%d\n", MaxHeap.top() );
MinHeap.push( MaxHeap.top() );
MaxHeap.pop();
}
}

代码如下(Treap):

#include <cstdio>
#include <ctime>
#include <cstring>
#include <iostream>
using namespace std; const int MAX_N = + ;
int add[MAX_N]; struct Node{
Node *ch[];
int value, key;
int size;
int cmp(int x) const{
if (x == value) return -;
return x < value ? : ;
}
void Maintain(){
size = ;
if (ch[] != NULL) size += ch[]->size;
if (ch[] != NULL) size += ch[]->size;
}
}; void Rotate(Node *&o, int d){
Node *k = o->ch[d ^ ];
o->ch[d ^ ] = k->ch[d];
k->ch[d] = o;
o->Maintain();
k->Maintain();
o = k;
} void Insert(Node *&o, int x){
if (o == NULL){
o = new Node();
o->ch[] = o->ch[] = NULL;
o->value = x;
o->key = rand();
}
else{
int d = (x < (o->value) ? : );
Insert(o->ch[d], x);
if (o->ch[d]->key > o->key)
Rotate(o, d ^ );
}
o->Maintain( );
} void Remove(Node *&o, int x){
int d = o->cmp(x); if (d == -){
if (o->ch[] == NULL)
o = o->ch[];
else if (o->ch[] == NULL)
o = o->ch[];
else{
int d2 = (o->ch[]->key > o->ch[]->key ? : );
Rotate(o, d2);
Remove(o->ch[d2], x);
}
}
else
Remove(o->ch[d], x);
} int Find(Node *o, int x){
while (o != NULL){
int d = o->cmp(x); if (d == -) return ;
else o = o->ch[d];
}
return ;
} int FindKth(Node *o, int k){
int l_size = (o->ch[] == NULL ? : o->ch[]->size);
if (k == l_size + )
return o->value;
else if (k <= l_size)
return FindKth(o->ch[], k);
else
return FindKth(o->ch[], k - l_size - );
} int main()
{
int m, n, k;
Node *root = NULL; srand();
scanf("%d %d", &m, &n);
for (int i = ; i <= m; ++i)
scanf("%d", &add[i]);
int j = ;
for (int i = ; i <= n; ++ i){
scanf("%d", &k); for (; j <= k; ++j)
Insert(root, add[j]);
printf("%d\n", FindKth(root, i));
}
return ;
}

poj 1442 Black Box(优先队列&Treap)的更多相关文章

  1. POJ 1442 Black Box(优先队列)

    题目地址:POJ 1442 这题是用了两个优先队列,当中一个是较大优先.还有一个是较小优先. 让较大优先的队列保持k个.每次输出较大优先队列的队头. 每次取出一个数之后,都要先进行推断,假设这个数比較 ...

  2. POJ 1442 Black Box -优先队列

    优先队列..刚开始用蠢办法,经过一个vector容器中转,这么一来一回这么多趟,肯定超时啊. 超时代码如下: #include <iostream> #include <cstdio ...

  3. POJ 1442 Black Box treap求区间第k大

    题目来源:POJ 1442 Black Box 题意:输入xi 输出前xi个数的第i大的数 思路:试了下自己的treap模版 #include <cstdio> #include < ...

  4. poj 1442 Black Box(堆 优先队列)

    题目:http://poj.org/problem?id=1442 题意:n,m,分别是a数组,u数组的个数,u[i]w为几,就加到a几,然后输出第i 小的 刚开始用了一个小顶堆,超时,后来看了看别人 ...

  5. [ACM] POJ 1442 Black Box (堆,优先队列)

    Black Box Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7099   Accepted: 2888 Descrip ...

  6. 优先队列 || POJ 1442 Black Box

    给n个数,依次按顺序插入,第二行m个数,a[i]=b表示在第b次插入后输出第i小的数 *解法:写两个优先队列,q1里由大到小排,q2由小到大排,保持q2中有i-1个元素,那么第i小的元素就是q2的to ...

  7. POJ 1442 Black Box 堆

    题目: http://poj.org/problem?id=1442 开始用二叉排序树写的,TLE了,改成优先队列,过了.. 两个版本都贴一下吧,赚稿费.. #include <stdio.h& ...

  8. POJ 1442 Black Box

    第k大数维护,我推荐Treap..谁用谁知道....                                                           Black Box Time ...

  9. 数据结构(堆):POJ 1442 Black Box

    Black Box Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10658   Accepted: 4390 Descri ...

随机推荐

  1. python 序列(list,tuple,str)基本操作

    添加元素: mylist.append() mylist.extend([1, 2]) mylist.insert(1, "pos") 删除元素: mylist.remove(va ...

  2. AngularJS Directive 学习笔记

    指令 Directive 指令要点 大漠老师的教学节点 解析最简单的指令 hello: 匹配模式 restrict 解析最简单的指令 hello: template.tempmlateUrl.$tem ...

  3. WPF datagrid 如何隔行变色

    <DataGrid AlternationCount="2"> <DataGrid.RowStyle> <Style TargetType=" ...

  4. 类中成员函数与数据成员private/pubic/protected

    类中成员函数与数据成员private/pubic/protected

  5. C++ STL中map存储方式——SAP电面(4)

    map存储方式  一般是平衡二叉树 红黑树

  6. malloc/free和new/delete的区别汇总

    一.基本概念 malloc/free 1.函数原型及说明: void* malloc(long NumBytes):该函数分配了NumBytes个字节,并返回了指向这块内存的指针.如果分配失败,则返回 ...

  7. linux怎样修改用户权限

    ls -l  查看目录下的文件详细信息 ll 同上 whoami 功能说明:先似乎用户名称. 语 法:whoami [--help][--version] 补充说明:显示自身的用户名称,本指令相当于执 ...

  8. C语言的指针变量

    C语言的指针变量 在C语言中,变量是固定范围的存储空间,它存储的是赋给他的值, 比如: ; /* 这里是定义一个整型变量a,并把12这个值存储在a的地址空间上 这个地址空间是系统随机分配的,对用户是透 ...

  9. 一次搞懂 Assets Pipeline 转载自http://gogojimmy.net/2012/07/03/understand-assets-pipline/

    Assets Pipeline 是 Rails 3.1 一個重要的功能,一直並沒有很去了解其特性,但因為最近都在寫前端的東西在 assets pipeline 的東西上跌跌撞撞了不少次(尤其在 dep ...

  10. ThinkPHP第十七天(隐藏index.php和简短路径配置)

    1.路由设置,让路径中不显示index.php方法: 第一步:在apache中的httpd.conf中查找: LoadModule rewrite_module modules/mod_rewrite ...