Black Box[優先佇列]
阿新 • • 發佈:2018-11-25
將前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(); } }