P1801 黑匣子

题面:https://www.luogu.org/problemnew/show/P1801

本题就是把元素先都放入大根堆,然后当大根堆中元素大于等于k个时,则把多余元素放入小根堆中,这样保证小根堆堆顶是第k小的
然后当输出完第k小的数后,则要把它放回大根堆(因为k增加了)

Code:
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int a[200005],g[200005];
priority_queue<int> B;
priority_queue<int,vector<int>,greater<int> > A;
int main(){
    int n,m,k=1;
    scanf("%d%d",&n,&m);
    for(int x=1;x<=n;x++){
        scanf("%d",&a[x]);
	}
    for(int x=1;x<=m;x++){
        scanf("%d",&g[x]);
	}
    for(int j=1;j<=n;j++){
        B.push(a[j]);
        if(B.size()>=k){
			A.push(B.top());
			B.pop();
		}
        while(j==g[k]){
            printf("%d\n",A.top());
            B.push(A.top());
            A.pop();
            k++;
        }
    }
    return 0;
}
posted @ 2019-07-16 13:44  prestige  阅读(161)  评论(0编辑  收藏  举报