题目链接: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. hough变换中,直线方程从XY空间转换到参数空间的转换过程

    XY空间直线方程:y=kx+b 参数空间直线方程:xcosθ+ysinθ=ρ 直线方程从XY空间转换到参数空间过程的转换过程: k=tan(π-α)=tan(-α)=-tanα=-cotθ=-cosθ ...

  2. 移动端开发(四):swiper.js

    swiper.js中文网:http://www.swiper.com.cn/ 文档结构 swiper.jquery.js    是需要引用jquery.js 或者 zepto.js 时,只需直接引用该 ...

  3. [Swust OJ 385]--自动写诗

    题目链接:http://acm.swust.edu.cn/problem/0385/ Time limit(ms): 5000 Memory limit(kb): 65535    Descripti ...

  4. 删除Lb重复的数,用La输出(顺序表)

    #include<stdio.h> typedef int A; const int LIST_INIT_SIZE=100; const int LISTINCRMENT=10; type ...

  5. 两种写法实现Session Scope的Spring Bean

    xml based: <bean id="localRepository" class="com.demo.bean.LocalRepository" s ...

  6. jquery回调函数callback的使用

    回调函数必须是全局函数或者静态函数,不可定义为某个特定的类的成员函数 callback函数在当前动画100%完成之后执行 $("p").hide(1000); alert(&quo ...

  7. jquery+easy ui 实现表格列头筛选

    示例代码 1.筛选的下拉 <a href="javascript:void(0)" id="filterStatus" class="easyu ...

  8. ural1057 Amount of degrees 位数统计

    #include <iostream> #include <string> using namespace std; ][]; void init(){ f[][] =; ;i ...

  9. C——货物管理系统

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> ...

  10. android screenOrientation

    Activity: android:screenOrientation 横(landscape)竖(portrait)屏显示. 如果想让它一直是横屏显示的话,xml:android:screenOri ...