动态中位数
依次读入一个整数序列,每当已经读入的整数个数为奇数时,输出已读入的整数构成的序列的中位数。
输入格式
第一行输入一个整数P,代表后面数据集的个数,接下来若干行输入各个数据集。
每个数据集的第一行首先输入一个代表数据集的编号的整数。
然后输入一个整数M,代表数据集中包含数据的个数,M一定为奇数,数据之间用空格隔开。
数据集的剩余行由数据集的数据构成,每行包含10个数据,最后一行数据量可能少于10个,数据之间用空格隔开。
输出格式
对于每个数据集,第一行输出两个整数,分别代表数据集的编号以及输出中位数的个数(应为数据个数加一的二分之一),数据之间用空格隔开。
数据集的剩余行由输出的中位数构成,每行包含10个数据,最后一行数据量可能少于10个,数据之间用空格隔开。
输出中不应该存在空行。
数据范围
1≤P≤1000,
1≤M≤9999
输入样例:
3
1 9
1 2 3 4 5 6 7 8 9
2 9
9 8 7 6 5 4 3 2 1
3 23
23 41 13 22 -3 24 -31 -11 -8 -7
3 5 103 211 -311 -45 -67 -73 -81 -99
-33 24 56
输出样例:
1 5
1 2 3 4 5
2 5
9 8 7 6 5
3 12
23 23 22 22 13 3 5 5 3 -3
-7 -3
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstring>
#include<map>
#include<queue>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
int p;
cin>>p;
while(p--)
{
int m,n;
cin>>m>>n;
priority_queue<int> max_heap;
priority_queue<int,vector<int>,greater<int> > min_heap;
cout<<m<<" "<<(n+1)/2<<endl;
int cnt=0;
for(int i=1;i<=n;i++)
{
int t;
cin>>t;
if(min_heap.empty())
min_heap.push(t);
else
{
if(t<min_heap.top())
max_heap.push(t);
else
min_heap.push(t);
while(max_heap.size()>min_heap.size())
{
min_heap.push(max_heap.top());
max_heap.pop();
}
while(min_heap.size()>max_heap.size()+1)
{
max_heap.push(min_heap.top());
min_heap.pop();
}
}
if(i%2==1)
{
cout<<min_heap.top()<<" ";
cnt++;
if(cnt%10==0)
cout<<endl;
}
}
cout<<endl;
}
}