更:正解是堆,应该是题目水了
题目大意: 给你n个数,每当输入奇数个数时,就输出前面这些数的中位数。
模拟即可,可以用stl库函数upper_bound() 快速求出第一个比当前输入值大的值,将该值已经往后位置的值全部后移一个位置,然后即可得到中位数。
AC代码:
#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <cmath>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define endl '\n'
#define PI cos(-1)
#define ll long long
#define INF 0x3f3f3f3f
#define mod 1000000009
#define lowbit(abcd) (abcd & (-abcd))
ll ans[1000010], ans_ct = 1;
ll p[1000010];
int main()
{
ll t;
cin >> t;
while (t--)
{
ans_ct = 1;
fill(p, p + 100001, 0);
ll pos = 1;
ll num, n;
cin >> num >> n;
for (int i = 1; i <= n; i++)
{
ll a;
scanf("%lld", &a);
ll temp = upper_bound(p, p + 1 + pos, a) - p;
for (int i = pos; i >= temp; i--)
p[i + 1] = p[i];
if (temp == pos + 1)
temp = pos;
if (temp == 0)
temp = 1;
p[temp] = a;
if (i % 2)
{
if (pos % 2 == 1)
ans[ans_ct++] = p[(pos + 1) / 2];
else
{
ll ttemp = (p[pos / 2] + p[pos / 2 + 1]) / 2;
if ((p[pos / 2] + p[pos / 2 + 1]) % 2)
ttemp++;
ans[ans_ct++] = temp;
}
}
pos++;
}
printf("%lld %lld\n", num, ans_ct - 1);
ll sss = 0;
for (int i = 1; i < ans_ct - 1; i++)
{
sss++;
if (sss % 10 == 0)
printf("%lld\n", ans[i]);
else
printf("%lld ", ans[i]);
}
printf("%lld\n", ans[ans_ct - 1]);
}
return 0;
}