题目链接: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. SGU 201 Non Absorbing DFA (DP)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题意:给出一个自动机,给出所有的转移,其中还有一个 ...

  2. linux中时间函数

    linux下常用时间类型有四种: time_t . struct   tm. struct  timeval .    struct   timespec 1.time_t   时间函数 time_t ...

  3. 嵌入式系统 Boot Loader

    基于嵌入式系统中的 OS 启动加载程序 ―― Boot Loader 的概念.软件设计的主要任务以及结构框架等内容.

  4. offsetParent 到底是哪一个?

    前言 温故而知新.遇到offsetParent这个知识点,发现书上讲的不够详细.于是看了看豪情的博客,发现讲得很具体,收藏一下. 正文 不同情况 没有已定位的父节点,且自身position:relat ...

  5. c语言中的字符数组与字符串

    1.字符数组的定义与初始化 字符数组的初始化,最容易理解的方式就是逐个字符赋给数组中各元素. char str[10]={ 'I',' ','a','m',' ',‘h’,'a','p','p','y ...

  6. BZOJ 4305: 数列的GCD( 数论 )

    对于d, 记{ai}中是d的倍数的数的个数为c, 那么有: 直接计算即可,复杂度O(NlogN+MlogM) --------------------------------------------- ...

  7. JAVA中把ResultSet转换成LIST

    项目中老是遇到数据库异常关闭的情况,真烦, 想用hibernate呢,那个玩意儿又太笨重,感慨C#和PHP的舒适方便性,模拟TP写了个数据处理层,将就用着先代码里有很多项目中的东西,不要直接COPY了 ...

  8. Apache RewriteCond RewriteRule 入门和Laravel去掉index.php

        Ci删除index.php办法: 创建.htaccess 文件放到网站的根目录下,文件中的内容如下: RewriteEngine onRewriteCond %{REQUEST_FILENAM ...

  9. CodeIgniter中的session处理

    在CI中你可以这样很简单地装载library:     $this->load->library('newclass');    注意系统中会有两个library目录,一个是system/ ...

  10. UBER司机奖励政策

    高峰时段: 早高峰:早6:30-8:30点 晚高峰:晚4:00-7:00点 *周六日只有晚高峰 其他时间均为平峰时段 滴滴快车单单2.5倍,注册地址:http://www.udache.com/如何注 ...