AcWing 106. 动态中位数
原题链接
中等
作者:
zhuqingyun
,
2024-12-06 22:11:33
,
所有人可见
,
阅读 1
#include <bits/stdc++.h>
using namespace std;
int p, n, m;
int main()
{
scanf("%d", &p);
while (p -- )
{
scanf("%d%d", &n, &m);
printf("%d %d\n", n, (m + 1) / 2);
int cnt = 0;
priority_queue<int, vector<int>, greater<int> > up;
priority_queue<int, vector<int>, less<int> > down;
for (int i = 1; i <= m; i ++ )
{
int x;
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());
cnt ++ ;
if (cnt % 10 == 0)
{
printf("\n");
}
}
}
if (cnt % 10)
{
printf("\n");
}
}
return 0;
}