习题册(堆)
好菜,这个的全名我都不知道,只知其意。
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> h[4];
//最开始写的h[3],想的是只分为1,2,3个堆;
//但是报错了!!
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
到现在也不清楚为什么要开四个,难道堆也是h[0]、h[1]、h[2]、h[3]
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
struct node{
int p, a, b;
}a[N];
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> h[4];
int n, m;
int st[N];
int main(){
std::ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i ++) cin >> a[i].p;
for(int i = 1; i <= n; i ++) cin >> a[i].a;
for(int i = 1; i <= n; i ++) cin >> a[i].b;
for(int i = 1; i <= n; i ++) {
h[a[i].a].push({a[i].p, i});
h[a[i].b].push({a[i].p, i});
}
cin >> m;
while(m--){
int x;
cin >> x;
int flag = 0;
if(h[x].size()){
while(h[x].size()){
if(st[h[x].top().second]) h[x].pop();//其他堆里面需要判断
else{
cout << h[x].top().first << " ";
st[h[x].top().second] = 1;
h[x].pop();
flag = 1;
break;
}
}
}
if(flag == 0) cout << -1 << " ";
}
}