POJ 1442 Black Box

mac2022-06-30  129

这个题用优先队列很好过。

题目大意:

给出两种操作如下:( ADD  GET )

    操作          当GET取第几大         所有已添加元素                                                           输出的元素

1 ADD(3) 0 3 2 GET 1 3 3 3 ADD(1) 1 1, 3 4 GET 2 1, 3 3 5 ADD(-4) 2 -4, 1, 3 6 ADD(2) 2 -4, 1, 2, 3 7 ADD(8) 2 -4, 1, 2, 3, 8 8 ADD(-1000) 2 -1000, -4, 1, 2, 3, 8 9 GET 3 -1000, -4, 1, 2, 3, 8 1 10 GET 4 -1000, -4, 1, 2, 3, 8 2 11 ADD(2) 4 -1000, -4, 1, 2, 2, 3, 8 用给出的输入实现以上操作。 具体方法是: 建立一个大顶堆,一个小顶堆。

具体见代码:

#include <stdio.h> #include <queue> using namespace std; int main() { int a[30005],i,j,n,m; while(scanf("%d%d",&n,&m)!=EOF) { int cut=0,x,c=0,t; for(i=0; i<n; i++) { scanf("%d",&a[i]); } priority_queue <int , vector <int> , less<int> > p; //大顶堆 priority_queue <int , vector <int> , greater<int> >q; for(i=0; i<m; i++) { scanf("%d",&x); while(c<x) { q.push(a[c]); c++; } while(!p.empty()&&p.top()>q.top()) //保证P的元素一定比Q小 { t=p.top(); p.pop(); p.push(q.top()); q.pop(); q.push(t); } printf("%d\n",q.top()); p.push(q.top()); q.pop(); } } return 0; }

转载于:https://www.cnblogs.com/lin375691011/p/3996796.html

相关资源:数据结构—成绩单生成器
最新回复(0)