堆-樹 Middle Number TOJ3515
阿新 • • 發佈:2018-12-12
#include <iostream> using namespace std; #define N 1000 struct heap{ int da[N],size; void init() { size=0; } void insert(int v,int id) { da[++size]=v; int tm=size; while(tm>1) { if(id) { if(da[tm]>da[tm/2]) swap(da[tm],da[tm/2]); else break; } else{ if(da[tm]<da[tm/2]) swap(da[tm],da[tm/2]); else break; } tm/=2; } } void update(int id,int cur) { int max=cur; if(id){ if(2*cur<=size&&da[2*cur]>da[max]) max=cur*2; if(2*cur+1<=size&&da[2*cur+1]>da[max]) max=cur*2+1; } else{ if(2*cur<=size&&da[2*cur]<da[max]) max=cur*2; if(2*cur+1<=size&&da[2*cur+1]>da[max]) max=cur*2+1; } if(max!=cur){ swap(da[cur],da[max]); update(id,max); } } int gettop(int id) { int tm=da[1]; if(size==1){ size=0; return tm; } da[1]=da[size--]; update(id,1); return tm; } }h[2]; void add(int v){ if(h[1].size==0){ h[1].insert(v,1); return ; } else if(h[0].size==0) { if(v<h[1].da[1]){ int tm=h[1].gettop(1); h[1].insert(v,1); v=tm; } h[0].insert(v,0); return; } int tm=h[1].da[1],tp=h[0].da[1]; if(h[1].size>h[0].size) { if(v<tm) { h[1].gettop(1); h[1].insert(v,1); h[0].insert(tm,0); } else h[0].insert(v,0); } else { if(v>tp) { h[0].gettop(0); h[0].insert(v,0); h[1].insert(tp,1); } else { h[1].insert(v,1); } } } int mid() { return h[1].da[1]; } int main() { char p[5]; int T,n,m,i,j; cin>>T; while(T--) { cin>>n; h[0].init(),h[0].init(); for(i=0;i<n;i++) { cin>>j; add(j); } cin>>m; while(m--) { cin>>p; if(p[0]=='a') { cin>>j; add(j); } else cout<<mid(); } } }