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 ...
随机推荐
- 【BZOJ4004】[JLOI2015]装备购买 贪心+高斯消元
[BZOJ4004][JLOI2015]装备购买 Description 脸哥最近在玩一款神奇的游戏,这个游戏里有 n 件装备,每件装备有 m 个属性,用向量zi(aj ,.....,am) 表示 ( ...
- 6.2.3-AbstractBeanFactory
AbstractBeanFactory体系: 这个抽象类中很重要的实现了BeanFactory中得一个方法,doGetBean(); @SuppressWarnings("unchecked ...
- Convex combination
en.wikipedia.org/wiki/Convex_combination 凸组合 In convex geometry, a convex combination is a linear co ...
- zabbix_get 命令介绍
zabbix_get 是 zabbix 服务端的一个命令,用于检测 agent 端的配置是否正确,可以很方便地知道 key 是否能正常获取到数据,在测试自定义监控的时候特别有用 [root@crazy ...
- JVM类加载器
系统中的类加载器 1.BootStrap ClassLoader a.启动ClassLoader b.加载rt.jar 2.Extension ClassLoader a.扩展ClassLoader ...
- twig 截取字符串
<p>{{content|slice(0,100)}}</p> slice()截取content变量值,从0到100
- https 请求发送 例子 tls && ssl
package com.dooioo.training.helper; import java.io.IOException; import java.io.UnsupportedEncodingEx ...
- 错误解决Error configuring application listener of class org.springframework.web.util.Log4jConfigListener(转发)
Spring MVC-----maven项目导入后启动tomcat出现如下错误 参考:http://blog.csdn.net/itlionwoo/article/details/17523371 解 ...
- 常见数据挖掘算法的Map-Reduce策略(1)
大数据这个名词是被炒得越来越火了,各种大数据技术层出不穷,做数据挖掘的也跟着火了一把,呵呵,现今机器学习算法常见的并行实现方式:MPI,Map-Reduce计算框架,GPU方面,grap ...
- c的详细学习(3)数据的输入输出
c语言没有专门的数据输入输出语句,而是通过调用系统提供的的标准输入/输出库函数来实现数据的输入和输出. (1)数据的输出: 注意:在使用标准的输入输出库函数时,使用编译预处理命令“#inclu ...