poj 1442 Black Box(优先队列&Treap)
题目链接: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)的更多相关文章
- POJ 1442 Black Box(优先队列)
题目地址:POJ 1442 这题是用了两个优先队列,当中一个是较大优先.还有一个是较小优先. 让较大优先的队列保持k个.每次输出较大优先队列的队头. 每次取出一个数之后,都要先进行推断,假设这个数比較 ...
- POJ 1442 Black Box -优先队列
优先队列..刚开始用蠢办法,经过一个vector容器中转,这么一来一回这么多趟,肯定超时啊. 超时代码如下: #include <iostream> #include <cstdio ...
- POJ 1442 Black Box treap求区间第k大
题目来源:POJ 1442 Black Box 题意:输入xi 输出前xi个数的第i大的数 思路:试了下自己的treap模版 #include <cstdio> #include < ...
- poj 1442 Black Box(堆 优先队列)
题目:http://poj.org/problem?id=1442 题意:n,m,分别是a数组,u数组的个数,u[i]w为几,就加到a几,然后输出第i 小的 刚开始用了一个小顶堆,超时,后来看了看别人 ...
- [ACM] POJ 1442 Black Box (堆,优先队列)
Black Box Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7099 Accepted: 2888 Descrip ...
- 优先队列 || POJ 1442 Black Box
给n个数,依次按顺序插入,第二行m个数,a[i]=b表示在第b次插入后输出第i小的数 *解法:写两个优先队列,q1里由大到小排,q2由小到大排,保持q2中有i-1个元素,那么第i小的元素就是q2的to ...
- POJ 1442 Black Box 堆
题目: http://poj.org/problem?id=1442 开始用二叉排序树写的,TLE了,改成优先队列,过了.. 两个版本都贴一下吧,赚稿费.. #include <stdio.h& ...
- POJ 1442 Black Box
第k大数维护,我推荐Treap..谁用谁知道.... Black Box Time ...
- 数据结构(堆):POJ 1442 Black Box
Black Box Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10658 Accepted: 4390 Descri ...
随机推荐
- linux网络编程之网络函数详解
1.epoll_create函数 函数声明:int epoll_create(int size) 该 函数生成一个epoll专用的文件描述符.它其实是在内核申请一空间,用来存放你想关注的socket ...
- webservice跨域上传图片
1.上传文件,在一般处理程序中处理 //1.接收post过来的文件 HttpPostedFile file = context.Request.Files[]; || file.ContentLeng ...
- 【转】Linq实现DataTable行列转换
出处:http://www.cnblogs.com/li-peng/ 转换前的table: 转换后的table: 代码里有详细的说明, 还有一些参数我都截图了下面有 using System;usin ...
- CodeForces 577A Multiplication Table 质因子数
题目:click here 题意:看hint就懂了 分析:数论小题,在n0.5时间里求n的质因子数 #include <bits/stdc++.h> using namespace std ...
- strstr 的使用
Problem E: Automatic Editing Source file: autoedit.{c, cpp, java, pas} Input file: autoedit.in Outpu ...
- 多线程笔记--原子操作Interlocked系列函数
前面写了一个多线程报数的功能,为了描述方便和代码简洁起见,只输出最后的报数结果来观察程序运行结果.这非常类似一个网站的客户访问统计,每个用户登录用一个线程模拟,线程运行时将一个表示计数的变量递增.程序 ...
- 八皇后问题-回溯法(MATLAB)
原创文章,转载请注明:八皇后问题-回溯法(MATLAB) By Lucio.Yang 1.问题描述 八皇后问题是十九世纪著名数学家高斯于1850年提出的.问题是:在8*8的棋盘上摆放8个皇后,使其不能 ...
- RAC ORA-12170 ora-12535/tns-12535
现象:开发人员抱怨RAC数据库出现了时连得上时连不上的情况,用SQLPLUS一试,果然有这样的情况: SQL> conn system/*******@bjyd 已连接. SQL> con ...
- ASP.NET jQuery 随笔 使用allValidator插件简单实现客户端验证功能
首先放出该插件的下载地址:http://pan.baidu.com/s/1Aa3yD,里面有帮助文档,详细了解可以自行下载学习,本章只讲解一些基本的验证功能,页面代码如下: <%@ Page L ...
- github 的分支操作
首先需要当前目录设置为仓库目录 一.创建本地分支 1.查看有哪些分支:git branch 2.创建一个分支:git branch name ,其中name是分支名 3.切换到分支:git chec ...