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

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

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

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

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

代码如下:

  1. #include <cstdio>
  2. #include <algorithm>
  3. using namespace std;
  4. const int maxn=3e4+5;
  5. int n,m;
  6. int a[maxn],u[maxn];
  7. int read() {
  8. int x=0,f=1;char ch=getchar();
  9. for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
  10. for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
  11. return x*f;
  12. }
  13. struct Heap {
  14. int tot;
  15. int tree[maxn];
  16. void ins(int v) {
  17. tree[++tot]=v;
  18. int pos=tot;
  19. while(pos>1) {
  20. if(tree[pos]<tree[pos>>1])
  21. swap(tree[pos],tree[pos>>1]),pos>>=1;
  22. else break;
  23. }
  24. }
  25. int pop() {
  26. int res=tree[1];
  27. tree[1]=tree[tot--];
  28. int pos=1,son=2;
  29. while(son<=tot) {
  30. if(son<tot&&tree[son|1]<tree[son])son|=1;
  31. if(tree[son]<tree[pos])
  32. swap(tree[son],tree[pos]),pos=son,son=pos<<1;
  33. else break;
  34. }
  35. return res;
  36. }
  37. }T1,T2;
  38. int main() {
  39. n=read(),m=read();
  40. for(int i=1;i<=n;i++)
  41. a[i]=read();
  42. for(int i=1;i<=m;i++)
  43. u[i]=read();
  44. sort(u+1,u+m+1);
  45. for(int i=1,st=1;i<=m;i++) {
  46. while(st<=u[i]) {
  47. if(a[st]>=T1.tree[1])T1.ins(a[st]);
  48. else T2.ins(-a[st]);
  49. st++;
  50. }
  51. while(T2.tot<i)T2.ins(-T1.pop());
  52. while(T2.tot>i)T1.ins(-T2.pop());
  53. printf("%d\n",-T2.tree[1]);
  54. }
  55. return 0;
  56. }

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. 【BZOJ2322】[BeiJing2011]梦想封印 高斯消元求线性基+DFS+set

    [BZOJ2322][BeiJing2011]梦想封印 Description 渐渐地,Magic Land上的人们对那座岛屿上的各种现象有了深入的了解. 为了分析一种奇特的称为梦想封印(Fantas ...

  2. ASP.NET MVC + ADO.NET EF 项目实战(一):应用程序布局设计

    什么叫上下文? 在你设计一个方法的时候,无法直接从方法参数或实例成员(字段或属性)获得的所有信息都是上下文.例如: 当前用户是谁? 刚才提供操作的数据库连接实例从哪里拿到? 这个方法从哪个 View ...

  3. [笔记]我的Linux入门之路 - 02.***-Qt5配置

    作为一个学习中的程序员,查wiki等,***肯定是刚需.况且没有它很多东西都下不下来.我在windows环境下使用的是shadowsocks,那么在linux下也使用它. 一.SS版本 SS版本众多, ...

  4. 让Xcode支持高版本系统设备真机测试

    最新支持11.2 (15C107) Xcode只可以支持iPhone手机对应iOS系统以下的真机测试.一般想要支持最新的iPhone手机系统,有两个方法. 第一.就需要更新Xcode,这一个方法有一个 ...

  5. [note]CRT&exCRT

    中国剩余定理 别人的blog 假设现在有关于x的同余方程组(p1,p2均为质数) \(x=a_1\pmod {p_1}\) \(x=a_2\pmod {p_2}\) 可以转化成如下形式 \(x=a_1 ...

  6. c#的const可以用于引用类型吗

    答案是可以的.不过用const修饰的类实例只能是null. class A{ public int a=0; } class B{ const A constA=null; const object ...

  7. 中国移动OnetNet云平台 使用以太网传输数据流步骤

    使用工具: 网络调试助手 链接:http://pan.baidu.com/s/1c06VC9E 密码:h0ys 1.选择TCP Client 2.输入IP   183.230.40.33 3.输入端口 ...

  8. pinpoint插件开发实践

    plugin基本结构 一个plugin主要由三部分构成,插件类增强定义(ProfilerPlugin接口实现).插件描述定义(TraceMetadataProvider接口实现).增强类拦截器实现(A ...

  9. 2018年长沙理工大学第十三届程序设计竞赛 G 逃离迷宫 【BFS】

    链接:https://www.nowcoder.com/acm/contest/96/G 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...

  10. PAT 天梯赛 L3-001. 凑零钱 【DP】【DFS】

    题目链接 https://www.patest.cn/contests/gplt/L3-001 思路 DP[I][J] I 表示第几个物品 J 表示多少钱 dp[i][j] 为 bool 值 表示 当 ...