0分!!

P1168 中位数

codejiahui @ 2022-10-23 11:07:06

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
priority_queue<int,vector<int>,greater<int>> q1;
priority_queue<int> q2;
int main()
{
    int n,x;
    scanf("%d%d",&n,&x);
    q1.push(x);
    printf("%d\n",x);
    for (int i = 2;i <= n;i += 2)
    {
        scanf("%d",&x);
        if (x <= q1.top())
            q2.push(x);
        else q1.push(x);
        scanf("%d",&x);
        if (x <= q1.top())
            q2.push(x);
        else q1.push(x);
        while (q1.size() > q2.size() + 1)
        {
            q2.push(q1.top());
            q1.pop();
        }
        while (q1.size() < q2.size() + 1)
        {
            q1.push(q2.top());
            q2.pop();
        }
        printf("%d\n",q1.top());
    }
    return 0;
}

|