看到各路dalao用平衡树的做法,表示本人不才,并不会。

然而我会优先队列_huaji_,并且发现用堆解题的dalao们并没有基于在线的做法

于是我的showtime到了

评测结果:https://www.luogu.org/record/show?rid=4645103

先讲一下思路:

题干所描述的** i **的实际值并不重要,他只是作为堆的一个位置

当然如果你暴力模拟的话肯定会超时

  • 为此我们考虑维护两个堆,一个是大根堆,一个是小根堆,且满足小根堆中的数比大根堆中的数大

  • 用bool变量flag来表示当前所操作的是大根堆还是小根堆

  • 首先是ADD(x),按flag和x分为四种情况

  • 然后是GET,按flag分为两种情况

  • 具体操作见以下代码:

  1. #include<cstdlib>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<ctime>
  6. #include<iostream>
  7. #include<vector>
  8. #include<list>
  9. #include<stack>
  10. #include<queue>
  11. #include<deque>
  12. #include<map>
  13. #include<set>
  14. #include<algorithm>
  15. #pragma GCC optimize(3)//STL必带
  16. using namespace std;
  17. #define ll long long
  18. const int maxn=2e5+10;
  19. const int INF=0x7fffffff;
  20. inline void read(int&x){//卡常的输入
  21. int data=0,w=1;
  22. char ch=getchar();
  23. while(ch!='-'&&!isdigit(ch))
  24. ch=getchar();
  25. if(ch=='-')
  26. w=-1,ch=getchar();
  27. while(isdigit(ch))
  28. data=10*data+ch-'0',ch=getchar();
  29. x=data*w;
  30. }
  31. void write(int x){//卡常的输出
  32. if(x<0)
  33. putchar('-'),x=-x;
  34. if(x>9)
  35. write(x/10);
  36. putchar('0'+x%10);
  37. }
  38. int a[maxn],u[maxn]={0};
  39. priority_queue <int,vector<int>,less<int> > lrh;//大根堆,large root heap
  40. priority_queue <int,vector<int>,greater<int> > srh;//小根堆,small root heap
  41. int main()
  42. {
  43. // freopen(".in","r",stdin);
  44. // freopen(".out","w",stdout);
  45. int m,n;
  46. read(m);read(n);
  47. for(int i=1;i<=m;++i)
  48. read(a[i]);
  49. int t;
  50. for(int i=1;i<=n;++i){
  51. read(t);
  52. ++u[t];
  53. }
  54. /*clog<<"u:"<<endl;
  55. for(int i=1;i<=m;++i)
  56. clog<<i<<" "<<u[i]<<endl;
  57. clog<<"uend"<<endl;*/
  58. bool flag=1;
  59. srh.push(INF); //这个细节很重要,INF的值对此题没有影响,但是如果不加对第一个数的操作会RE的
  60. #define x a[i]
  61. #define y u[i]
  62. for(int i=1;i<=m;++i){
  63. if(flag)//ADD(x)
  64. if(x<srh.top()){
  65. lrh.push(x);
  66. flag=0;
  67. }
  68. else
  69. srh.push(x);
  70. else
  71. if(x<lrh.top()){//这一步是ADD的精华所在,对两个对做了等价变换
  72. srh.push(lrh.top());//i值前移,因而大根堆要把最大数给小根堆
  73. lrh.pop();
  74. lrh.push(x);
  75. }
  76. else
  77. srh.push(x);
  78. while(y--){//GET
  79. // clog<<"once in "<<i<<endl;
  80. if(flag){//这一步是GET的精华所在,对两个对做了等价变换
  81. write(srh.top());putchar('\n');
  82. lrh.push(srh.top());//i值后移,因而小根堆要把最小数给大根堆
  83. srh.pop();
  84. }
  85. else{
  86. write(lrh.top());putchar('\n');
  87. flag=1;
  88. }
  89. }
  90. }
  91. // fclose(stdin);
  92. // fclose(stdout);
  93. return 0;
  94. }

LG1801 【黑匣子_NOI导刊2010提高(06)】的更多相关文章

  1. P1801 黑匣子_NOI导刊2010提高(06)

    P1801 黑匣子_NOI导刊2010提高(06) 题目描述 Black Box是一种原始的数据库.它可以储存一个整数数组,还有一个特别的变量i.最开始的时候Black Box是空的.而i等于0.这个 ...

  2. Luogu P1801 黑匣子_NOI导刊2010提高(06)

    P1801 黑匣子_NOI导刊2010提高(06) 题目描述 Black Box是一种原始的数据库.它可以储存一个整数数组,还有一个特别的变量i.最开始的时候Black Box是空的.而i等于0.这个 ...

  3. 洛谷 P1801 黑匣子_NOI导刊2010提高(06)(未完)

    P1801 黑匣子_NOI导刊2010提高(06) 题目描述 Black Box是一种原始的数据库.它可以储存一个整数数组,还有一个特别的变量i.最开始的时候Black Box是空的.而i等于0.这个 ...

  4. 【洛谷】【堆】P1801 黑匣子_NOI导刊2010提高(06)

    [题目描述:] Black Box是一种原始的数据库.它可以储存一个整数数组,还有一个特别的变量i.最开始的时候Black Box是空的.而i等于0.这个Black Box要处理一串命令. 命令只有两 ...

  5. 题解 P1801 【黑匣子_NOI导刊2010提高(06)】

    蒟蒻来发题解了.我仔细看了一下其他题解,各位巨佬用了堆,红黑树,splay,treap之类的强大算法,表示蒟蒻的我只会口胡这些算法,所以我决定用一种极其易理解的算法————fhq treap,作为tr ...

  6. 【luogu P1801 黑匣子_NOI导刊2010提高(06)】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1801 替罪羊树吼啊! #include <cstdio> #include <cstrin ...

  7. [洛谷P1801]黑匣子_NOI导刊2010提高(06)

    题目大意:两个操作:向一个可重集中加入一个元素:询问第$k$大的数($k$为之前询问的个数加一) 题解:离散化,权值线段树直接查询 卡点:无 C++ Code: #include <cstdio ...

  8. 洛谷 P1801 黑匣子_NOI导刊2010提高(06)

    题目描述 Black Box是一种原始的数据库.它可以储存一个整数数组,还有一个特别的变量i.最开始的时候Black Box是空的.而i等于0.这个Black Box要处理一串命令. 命令只有两种: ...

  9. 黑匣子_NOI导刊2010提高(06) Splay Tree

    题目描述 Black Box是一种原始的数据库.它可以储存一个整数数组,还有一个特别的变量i.最开始的时候Black Box是空的.而i等于0.这个Black Box要处理一串命令. 命令只有两种: ...

随机推荐

  1. spring boot 启动报错(spring-boot-devtools热部署后):The elements [spring.resources.cache-period] were left unbound. Update your application's configuration

    详细错误代码: *************************** APPLICATION FAILED TO START *************************** Descript ...

  2. 第五章 [BX]和loop指令

    5.1 [bx] [bx]是什么 和 [0] 有些类似,[0] 表示内存单元,它的偏移地址是 0. 例如: mov ax, [0] 内存以字节为单位:ax以字(16bit = 2Byte)为单位:al ...

  3. JS-构造函数2

    一.如何创建对象 1.对象字面量 var obj1={ name:"吻别", singer:"张学友", type:"流行" } 2.构造函 ...

  4. hdu-2227-dp+bit

    Find the nondecreasing subsequences Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/3 ...

  5. hdu-2509-反nim博弈

    Be the Winner Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  6. POJ-2689 Prime Distance (两重筛素数,区间平移)

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13961   Accepted: 3725 D ...

  7. Razor视图引擎 语法学习

    下面就和大家分享下我在asp.net官网看到的资料,学习到的点语法.1.通过使用@符号,可以直接在html页面中写C#或者VB代码:运行后: 2.页面中的C#或者VB代码都放在大括号中.运行后: 3. ...

  8. transition多个属性同时渐变(left/top)

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    < ...

  9. spring cloud 学习(一)初学SpringCloud

    初学SpringCloud 前言 在SpringBoot的坑还没填完的情况下,我又迫不及待地开新坑了.主要是寒假即将结束了,到时又得忙于各种各样的事情……留个坑给自己应该就会惦记着它,再慢慢地补上…… ...

  10. apache-service的使用

    apache service目录设置 设置Apache HTTP Server的文件根目录(DocumentRoot) 安装Apache 时,系统会给定一个缺省的文件根目录 如果你觉得这个网页存在这个 ...