在线求中位数(优先队列实现) POJ3784
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <queue> 5 6 using namespace std; 7 8 priority_queue<int,vector<int>,greater<int> >pq2; 9 priority_queue<int> pq1; 10 11 int main() 12 { 13 int T; 14 cin>>T; 15 int time; 16 int m; 17 while(T--) 18 { 19 while(!pq1.empty()) 20 pq1.pop(); 21 while(!pq2.empty()) 22 pq2.pop(); 23 int x; 24 cin>>time>>m; 25 cout<<time<<" "<<(m+1)/2<<endl; 26 cin>>x; 27 int mid=x; 28 cout<<mid; 29 for(int i=2;i<=m;i++) 30 { 31 cin>>x; 32 if(x>mid) 33 pq2.push(x); 34 else 35 pq1.push(x); 36 if(i%2==1) 37 { 38 if(pq1.size()==(pq2.size()+2)) 39 { 40 pq2.push(mid); 41 mid=pq1.top(); 42 pq1.pop(); 43 } 44 else if((pq1.size()+2)==pq2.size()) 45 { 46 pq1.push(mid); 47 mid=pq2.top(); 48 pq2.pop(); 49 } 50 if(i%20==1) 51 cout<<mid; 52 else 53 cout<<" "<<mid; 54 } 55 if(i%20==19) 56 cout<<endl; 57 } 58 cout<<endl; 59 } 60 return 0; 61 }