【POJ1442】Black Box

这道题可以用Splay、fhq-treap解决,但是平衡树解这道题大材小用,所以我采用了对顶堆解决。

考虑建立两个堆:以i为分割点,用大根堆存储前半段序列,用小根堆存储后半段序列。我们控制大根堆的元素个数为i,这样查询时大根堆的堆顶就是排名为i的元素。然后我们考虑维护对顶堆。

对于add操作,我们先将元素放入下面的大根堆内,从堆顶不断取出元素放到小根堆直到大根堆元素个数为i,这样大根堆的根就是第i小的元素,同时可以保证对顶堆的性质;
同理,对于get操作,我们先输出大根堆的根,然后将小根堆的根移到大根堆(因为i每次加一,要始终保证大根堆有i个元素),这样仍保证对顶堆的性质不变
因此,时间复杂度为O(nlog2n),我们用STL的优先队列即可完成。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <queue>
 6 using namespace std;
 7 typedef long long ll;
 8 int n,m,a[200010];
 9 priority_queue<int> maxx;
10 priority_queue<int,vector<int>,greater<int> >minn;
11 int main() {
12     scanf("%d%d",&n,&m);
13     for(int i=1;i<=n;i++)
14         scanf("%d",&a[i]);
15     int now=1;
16     for(int i=1;i<=m;i++) {
17         int x;
18         scanf("%d",&x);
19         for(int j=now;j<=x;j++) {
20             maxx.push(a[j]);
21             if(maxx.size()==i) minn.push(maxx.top()),maxx.pop();
22         }
23         now=x+1;
24         printf("%d\n",minn.top());
25         maxx.push(minn.top()); 
26         minn.pop();
27     }
28     return 0;
29 }
AC Code

 

转载于:https://www.cnblogs.com/shl-blog/p/10887188.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The 21st century is the society of information and new technologies: it wouldn’t be possible without the enormous software industry that is the foundation for it. However, software developers don’t exploit all the opportunities to perform a successful professional career, making the same mistakes over and over again. A good software project has to do more with the creative and artistic skills than the technical skills. The Black Book of the Programmer shows what distinguishes a neophyte programmer from the one that acts and works professionally. In the era of entrepreneurship and the new economy, the professional development of software is a fundamental pillar. If as a programmer you want to be not only good but professional, you can’t stop knowing the gems of wisdom that contains The Black Book of the Programmer. 21世纪是信息和新技术的社会:没有庞大的软件产业是它的基础,这是不可能的。但是,软件开发人员并没有利用所有机会来完成一个成功的职业生涯,一遍又一遍地犯同样的错误。一个好的软件项目必须在创造性和艺术技能方面做得更多,而不是技术技能。程序员的黑皮书展示了新手程序员与专业程序员的区别。在创业和新经济时代,软件的专业发展是一个基本支柱。如果作为一名程序员,你不仅要做得好而且要专业,那么你就不能停止了解包含程序员黑皮书的智慧宝石。有关的更多信息www.rafablanes.com

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值