black box(大根堆小根堆)
1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 using namespace std; 5 int a[30010],b[30010],ha[30010],hb[30010],na,nb; 6 void upa(int p){ 7 while(p>1){ 8 if(ha[p]<ha[p/2]){ 9 swap(ha[p],ha[p/2]); 10 p/=2; 11 } 12 else break; 13 } 14 } 15 void inserta(int val){ 16 ha[++na]=val; 17 upa(na); 18 } 19 void upb(int p){ 20 while(p>1){ 21 if(hb[p]>hb[p/2]){ 22 swap(hb[p],hb[p/2]); 23 p/=2; 24 } 25 else break; 26 } 27 } 28 void insertb(int val){ 29 hb[++nb]=val; 30 upb(nb); 31 } 32 void downb(int p){ 33 int s=p*2; 34 while(s<=nb){ 35 if(s<nb&&(hb[s]<hb[s+1]))s++; 36 if(hb[s]>hb[p]){ 37 swap(hb[s],hb[p]); 38 p=s;s=2*p; 39 } 40 else break; 41 } 42 } 43 void downa(int p){ 44 int s=p*2; 45 while(s<=na){ 46 if(s<na&&(ha[s]>ha[s+1]))s++; 47 if(ha[s]<ha[p]){ 48 swap(ha[s],ha[p]); 49 p=s;s=2*p; 50 } 51 else break; 52 } 53 } 54 void shiftb(){ 55 inserta(hb[1]); 56 hb[1]=hb[nb--]; 57 downb(1); 58 } 59 void geta(){ 60 cout<<ha[1]<<endl; 61 insertb(ha[1]); 62 ha[1]=ha[na--]; 63 downa(1); 64 } 65 int main(){ 66 int A,B,I=1,m=1; 67 cin>>A>>B; 68 for(int i=1;i<=A;i++)cin>>a[i]; 69 for(int i=1;i<=B;i++)cin>>b[i]; 70 for(int i=1;i<=A;i++){ 71 inserta(a[i]); 72 while(ha[1]!=0&&ha[1]<hb[1]){ 73 swap(ha[1],hb[1]); 74 downa(1);downb(1); 75 } 76 while(i==b[m]){ 77 geta(); 78 m++; 79 } 80 if(!b[m])break; 81 } 82 return 0; 83 }