Black Box[优先队列]

传送门

将前i-1小放在第一个小根堆(大的在上)

后面的放在第二个大根堆(小的在上) , 每次插入到大根堆里 , 不满足顺序就将大小两个的top交换

每次完了过后 , 放一个到小根堆就好了


#include<cstdio>
#include<queue>
#define N 30050
using namespace std;
int n,a[N],m,now;
priority_queue<int>L; 
priority_queue<int,vector<int>,greater<int> >R;
void Insert(int val){
	R.push(val);
	while(!L.empty() && L.top()>R.top()){
		int x=L.top(),y=R.top(); L.pop(); R.pop();
		R.push(x) , L.push(y);
	}
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=1;i<=m;i++){
		int x; scanf("%d",&x);
		while(now<x) Insert(a[++now]);
		printf("%d\n",R.top()); 
		L.push(R.top()) , R.pop();
	}
}

猜你喜欢

转载自blog.csdn.net/sslz_fsy/article/details/84439251