# 题目
依次读入一个整数序列,每当已经读入的整数个数为奇数时,输出当前中位数
# 题解
建立两个堆,一个大根堆,一个小根堆,
小根堆的个数小于等于大根堆个数+1
每次如果小根堆的为空插入,如果当前插入的数小于小根堆堆顶部,插入大根堆,然后判断两个堆的个数是否合法,不合法调换,小根堆堆顶的即当前的中位数
1 #include<bits/stdc++.h> 2 #define pii pair<int,int> 3 #define pb push_back 4 #define fi first 5 #define se second 6 using namespace std; 7 const int N=1e5+10; 8 int t,n; 9 int main(){ 10 ios::sync_with_stdio(0); 11 cin.tie(0); 12 cout.tie(0); 13 cin>>t; 14 int idx; 15 while(t--){ 16 cin >>idx>>n; 17 cout<<idx<<' '<<n/2+1<<endl; 18 priority_queue<int,vector<int>,greater<int>>min_; 19 priority_queue<int,vector<int>>max_; 20 int cnt=0; 21 for(int i = 1; i <= n; i++){ 22 int x; 23 cin >> x; 24 if(min_.empty()) 25 min_.push(x); 26 else if(!min_.empty() && x < min_.top()) 27 max_.push(x); 28 else 29 min_.push(x); 30 31 if(min_.size()>max_.size()+1){ 32 int tmp=min_.top(); 33 min_.pop(); 34 max_.push(tmp); 35 } 36 if(max_.size()>min_.size()){ 37 int tmp=max_.top(); 38 max_.pop(); 39 min_.push(tmp); 40 } 41 if(i&1){ 42 cnt++; 43 cout<<min_.top()<<' '; 44 } 45 if(cnt==10){ 46 cout<<endl; 47 cnt=0; 48 } 49 } 50 cout<<endl; 51 } 52 }