题目链接:动态中位数
AC代码:
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int main()
{
int T;
scanf("%d", &T);
while (T--) {
int n, m;
scanf("%d%d", &n, &m);
printf("%d %d\n", n, (m + 1) / 2);
int x;
priority_queue<int> down;//大顶堆
priority_queue<int,vector<int>,greater<int>> up;//小顶堆
int cnt = 0;
for (int i = 1; i <= m; i++)
{
scanf("%d", &x);
if (down.empty() || x <= down.top()) down.push(x);
else up.push(x);
if (down.size() > up.size() + 1) up.push(down.top()),down.pop();
if (up.size() > down.size()) down.push(up.top()),up.pop();
if (i % 2)
{
printf("%d ", down.top());
if (++cnt % 10 == 0) printf("\n");
}
}
if (cnt % 10) printf("\n");
}
system("pause");
return 0;
}