0分求调

P1168 中位数

codejiahui @ 2022-10-23 09:30:56

#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;
}

by muyangli @ 2022-11-06 08:10:25

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#define int long long
using namespace std;
priority_queue<int,vector<int>,greater<int> > q1;
priority_queue<int> q2;
signed main()
{
    int n,x;
    cin >> n >> x;
    q1.push(x);
    cout << x << endl;
    for (int i = 1;i <= n - 2;i += 2)//多输出一行 
    {
        cin >> x;
        if (x <= q1.top())
            q2.push(x);
        else q1.push(x);
        cin >> 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();
        }
        cout << q1.top() << endl;
    }
    if(n % 2 == 0) cin >> x;

    return 0;
}

by muyangli @ 2022-11-06 09:14:40

@codejiahui


|