升序堆和降序堆(优先队列) 洛谷1801

 1 // 洛谷1801 
 2 // 一个升序堆,一个降序堆
 3 // 降序堆维护序列的前i个最小值
 4 // 插如元素的时候,如果x小于降序堆最大值,则替换,并将最大值插入升序堆;否则,直接插入升序堆
 5 // 每次输出升序堆的最小值即可
 6 
 7 
 8 #include <bits/stdc++.h>
 9 using namespace std;
10 #define LL long long
11 typedef pair<int,int> pii;
12 const int inf = 0x3f3f3f3f;
13 const int N =1e3+50;
14 #define clc(a,b) memset(a,b,sizeof(a))
15 const double eps = 1e-8;
16 void fre() {freopen("in.txt","r",stdin);}
17 void freout() {freopen("out.txt","w",stdout);}
18 inline int read() {int x=0,f=1;char ch=getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch=getchar();}while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}return x*f;}
19 
20 priority_queue<int> qmax;
21 priority_queue<int,vector<int>,greater<int> > qmin;
22 int a[300010];
23 int main(){
24     int n,m;
25     scanf("%d%d",&n,&m);
26     for(int i=1;i<=n;i++) scanf("%d",&a[i]);
27     int j=1;
28     for(int i=1;i<=m;i++){
29         int x;
30         scanf("%d",&x);
31         for(;j<=x;j++)
32         if(!qmax.empty()&&a[j]<qmax.top()){
33             qmin.push(qmax.top());
34             qmax.pop();
35             qmax.push(a[j]);
36         }
37         else qmin.push(a[j]);
38         printf("%d
",qmin.top());
39         qmax.push(qmin.top());
40         qmin.pop();
41     }
42     return 0;
43 }
原文地址:https://www.cnblogs.com/ITUPC/p/5929780.html