题目链接: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. Android基础之在Eclipes中关联SDK源码和查看SDK源码

    在进行Android应用开发的时候,我们有时候需要查看某个类或接口的源码从而了解如何去使用一个类或者实现一个接口,查看源码有助于我们的学习某个封装的类的底层是如何实现的,这样可以帮助我们掌握类或者接口 ...

  2. js中this指向问题

    1.在全局范围内使用this的时候  它指向的是全局对象 this Window {top: Window, window: Window, location: Location, external: ...

  3. Android消息机制之Handler

    Android为什么要提供Handler Android建议我们不要在UI线程中执行耗时操作,因为这很容易导致ANR异常(在Android源码中我们可以看到,UI如果对用户的操作超过5秒无响应,就会报 ...

  4. hough变换是如何检测出直线和圆的?

    (I)直线篇 1 直线是如何表示的?对于平面中的一条直线,在笛卡尔坐标系中,常见的有点斜式,两点式两种表示方法.然而在hough变换中,考虑的是另外一种表示方式:使用(r,theta)来表示一条直线. ...

  5. BZOJ 3444: 最后的晚餐( )

    把暗恋关系看成无向边, 那某个点度数超过2就无解.存在环也是无解.有解的话对连通分量进行排列就行了. ------------------------------------------------- ...

  6. ssh免密钥登录

    说明:下文中说的 '客户端'指的是你所使用的本地机器; '服务端'指的是远程你要连接的机器; ----------------------------------------------------- ...

  7. Grunt.js 上手

    Official Site gruntjs.org/docs/getting-started.html 或者看http://tgideas.qq.com/webplat/info/news_versi ...

  8. BI商业智能项目中的若干风险要素

    BI商业智能项目应在 “业务驱动,总体规划,统一设计,分期实施” 的总体设计原则下分期实施,采取Agile BI方法论迭代开展,先确保核心功能满足客户需求,在总体规划下不断完善整个系统,以提高可交付性 ...

  9. 网络收发之cycleBuf

    #pragma once #include <iostream> #include <string> class cyclebuffer { protected: volati ...

  10. SharePoint迁移数据到生产环境

    SharePoint迁移数据到生产环境步骤如下: 1. 安装部署好生产环境 2. 配置管理中心 3. 安装SPD工具 4. 备份数据库(放在数据库服务器) 5. 备份wsp包(部署在管理中心服务器) ...