浅谈堆:https://www.cnblogs.com/AKMer/p/10284629.html

题目传送门:http://poj.org/problem?id=1442

用对顶堆维护第\(k\)小即可。保持小根堆大小逐渐递增就行。

时间复杂度:\(O(mlogn)\)

空间复杂度:\(O(n)\)

代码如下:

#include <cstdio>
#include <algorithm>
using namespace std; const int maxn=3e4+5; int n,m;
int a[maxn],u[maxn]; int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
} struct Heap {
int tot;
int tree[maxn]; void ins(int v) {
tree[++tot]=v;
int pos=tot;
while(pos>1) {
if(tree[pos]<tree[pos>>1])
swap(tree[pos],tree[pos>>1]),pos>>=1;
else break;
}
} int pop() {
int res=tree[1];
tree[1]=tree[tot--];
int pos=1,son=2;
while(son<=tot) {
if(son<tot&&tree[son|1]<tree[son])son|=1;
if(tree[son]<tree[pos])
swap(tree[son],tree[pos]),pos=son,son=pos<<1;
else break;
}
return res;
}
}T1,T2; int main() {
n=read(),m=read();
for(int i=1;i<=n;i++)
a[i]=read();
for(int i=1;i<=m;i++)
u[i]=read();
sort(u+1,u+m+1);
for(int i=1,st=1;i<=m;i++) {
while(st<=u[i]) {
if(a[st]>=T1.tree[1])T1.ins(a[st]);
else T2.ins(-a[st]);
st++;
}
while(T2.tot<i)T2.ins(-T1.pop());
while(T2.tot>i)T1.ins(-T2.pop());
printf("%d\n",-T2.tree[1]);
}
return 0;
}

POJ1442:Black Box的更多相关文章

  1. 电子产品使用感受之———我用过的最昂贵的手机壳:otter box 和 Apple 原装清水壳的对比

    2014年9月27日,我买到了我所使用的第一部 iPhone — iPhone 5C 蓝色.今天,2019年3月2日,我手里拿的是iPhoneXR 蓝色,两款手机如出一辙的设计和手感,让我充满了无限的 ...

  2. 【vagrant】第一次安装添加box报错:The box failed to unpackage properly....

    报错信息 The box failed to unpackage properly. Please verify that the box file you're trying to add is n ...

  3. 问题:Virtual Box如何复制镜像

    今天遇到的情况就是vagrant启动的默认Virtalbox镜像变了,大致可以处理的方法是 1 修改vagrant的默认virtalbox 2 重新在这个新的virtualbox中安装需要的软件 3 ...

  4. 论文阅读笔记四十八:Bounding Box Regression with Uncertainty for Accurate Object Detection(CVPR2019)

    论文原址:https://arxiv.org/pdf/1809.08545.pdf github:https://github.com/yihui-he/KL-Loss 摘要 大规模的目标检测数据集在 ...

  5. 计蒜客:Entertainment Box

    Ada, Bertrand and Charles often argue over which TV shows to watch, and to avoid some of their fight ...

  6. 【poj1442】Black Box

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10890   Accepted: 4446 Description Our ...

  7. IBM云的商业动作之我见(1):IBM 收购 OpenStack 托管私有云公司 Blue Box [IBM Acquired Blue Box]

    2015-06-10 IBM 刚刚(2015/06/03)宣布收购 Blue Box 公司.本文就聊聊这点事. 1. Blue Box 是做什么的?它是一家中小型托管私有云提供商. 1.1 公司的简单 ...

  8. [UE4]Scale Box:缩放容器

    一.Scale Box只能有一个子控件,再拖放一控件进去是不行的. 二.Scale Box缩放保持长宽比例 三. Scale Box.Strectching.Strectch:拉伸设置.  Scale ...

  9. poj-1442 Black Box(Treap)

    题目链接: Black Box 题意: 给一个序列,m个询问,每个询问是求前x个数中的第i小是多少; 思路: Treap的入门题目;Treap能实现STL的set实现不了的功能,如名次树(rank t ...

随机推荐

  1. Java编码规范之数据对象命名

    数据对象分多种,为方便阅读并区分各数据对象的用途,习惯将数据对象分为以下几类,供参考: 持久对象 PO(persistant object)对象关系映射(ORM)概念的产物,基本上对象的成员变量对应了 ...

  2. Unicode Category

    Lu(letter,uppercase):大写字母. Ll(letter.lowercase):小写字母. Lt(letter,titlecase):词首字母大写的字母. Lm(letter,modi ...

  3. IE11 for Windows 7 Enterprise With SP1 故障

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/jaminwm/article/details/29592027 这个故障非常诡异,卸载IE11也没实 ...

  4. easyui的 一些经验

    1. 渲染网络表格时,行操作 <th field="sort_num" width="10" data-options="field:'id', ...

  5. 遇到IIS configuration error错误的可以看看,不一定是权限问题

    最近接手了别人的一个 DOT NET项目,编译.调试一切都OK(心里暗暗高兴),发布吧,结果放到服务器上一运行出现Configuration Error错误,提示:“Access to the pat ...

  6. Android TableLayout 表格布局

    TableLayout继承LinearLayout 有多少个TableRow对象就有多少行, 列数等于最多子控件的TableRow的列数 直接在TableLayout加控件,控件会占据一行 Table ...

  7. DNS 原理入门 (转)

    DNS 是互联网核心协议之一.不管是上网浏览,还是编程开发,都需要了解一点它的知识. 本文详细介绍DNS的原理,以及如何运用工具软件观察它的运作.我的目标是,读完此文后,你就能完全理解DNS. 一.D ...

  8. nginx rewrite标签配置以及用户认证配置

    一.nginx  rewrite标签 rewrite 实现URL的改写主要是实现伪静态 1.  rewrite指令语法 指令语法:rewrite regex replacement[flag] 默认值 ...

  9. 20145229《Java程序设计》第四次实验报告

    Android开发基础 实验要求 1.基于Android Studio开发简单的Android应用并部署测试; 2.了解Android组件.布局管理器的使用: 3.掌握Android中事件处理机制. ...

  10. 20145229吴姗珊 《Java程序设计》第3周学习总结

    20145229吴姗珊 <Java程序设计>第3周学习总结 教材学习内容总结 第四章 认识对象 4.1类与对象 1.对象(Object):存在的具体实体,具有明确的状态和行为 2.类(Cl ...