POJ1442:Black Box
浅谈堆: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的更多相关文章
- 电子产品使用感受之———我用过的最昂贵的手机壳:otter box 和 Apple 原装清水壳的对比
2014年9月27日,我买到了我所使用的第一部 iPhone — iPhone 5C 蓝色.今天,2019年3月2日,我手里拿的是iPhoneXR 蓝色,两款手机如出一辙的设计和手感,让我充满了无限的 ...
- 【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 ...
- 问题:Virtual Box如何复制镜像
今天遇到的情况就是vagrant启动的默认Virtalbox镜像变了,大致可以处理的方法是 1 修改vagrant的默认virtalbox 2 重新在这个新的virtualbox中安装需要的软件 3 ...
- 论文阅读笔记四十八: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 摘要 大规模的目标检测数据集在 ...
- 计蒜客:Entertainment Box
Ada, Bertrand and Charles often argue over which TV shows to watch, and to avoid some of their fight ...
- 【poj1442】Black Box
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10890 Accepted: 4446 Description Our ...
- IBM云的商业动作之我见(1):IBM 收购 OpenStack 托管私有云公司 Blue Box [IBM Acquired Blue Box]
2015-06-10 IBM 刚刚(2015/06/03)宣布收购 Blue Box 公司.本文就聊聊这点事. 1. Blue Box 是做什么的?它是一家中小型托管私有云提供商. 1.1 公司的简单 ...
- [UE4]Scale Box:缩放容器
一.Scale Box只能有一个子控件,再拖放一控件进去是不行的. 二.Scale Box缩放保持长宽比例 三. Scale Box.Strectching.Strectch:拉伸设置. Scale ...
- poj-1442 Black Box(Treap)
题目链接: Black Box 题意: 给一个序列,m个询问,每个询问是求前x个数中的第i小是多少; 思路: Treap的入门题目;Treap能实现STL的set实现不了的功能,如名次树(rank t ...
随机推荐
- Struts中类型转换踩的坑
出现的异常: 当我输入的数据很大时候,转换后如上,这并不是我想要的, 出现问题的原因: Struts2对常用的数据类型如String.Integer.Double等都添加了转换器进行对应的转换操作. ...
- appium报'Command 'D\:\\android-sdk-windows\\platform-tools\\adb.exe -P 5037 -s “adb device” shell pm clear appPackage' exited with code 1'
解决方法:是因为手机开发者模式没有允许USB调试(安全模式),打开即可
- codeforces Gravity Flip 题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- (扫盲)RPC远程过程调用
https://blog.csdn.net/mindfloating/article/details/39473807 https://blog.csdn.net/mindfloating/artic ...
- linux.1:创建分区和文件系统
概述 使用 fdisk.gdisk 和 parted 创建和修改 MBR 和 GPT 分区在本教程中,学习磁盘分区和 Linux 文件系统相关内容,包括学习如何: 使用 mkfs 命令设置 ext2. ...
- HDU - 5695 Gym Class 【拓扑排序】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5695 思路 给定一些关系 进行拓扑排序 但是有一个要求 对于哪些没有确切的位置的点 要按照ID大小 I ...
- iOS UITableView滚动头图 拉伸放大效果 (头部弹性效果) 增加iOS11支持 附有demo
今天修改日期为2017年11月25日 两个月前做了iOS11的bug修复,才对博客进行更新,见谅. 在iOS11上需要注意两个问题 1.使用UIScrollview,UITableView,UIWeb ...
- Data Structure Binary Tree: Inorder Tree Traversal without Recursion
http://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion/ #include <iostream> #in ...
- 【Flask】模板继承
# 模版继承笔记: ### 为什么需要模版继承:模版继承可以把一些公用的代码单独抽取出来放到一个父模板中.以后子模板直接继承就可以使用了.这样可以重复性的代码,并且以后修改起来也比较方便. ### 模 ...
- <再看TCP/IP第一卷>关于网络层及协议细节---IP协议(2)--移动IP及DHCP
题外话:本来想按照互联网的层级自下向上回顾这些协议的,但是今天实在得破个例,DHCP不得不说! 主机从一个网络迁移到另一个网络,那么IP编址的结构就要发生改变,当今主流有如下几种修改方案: (一)改变 ...