优先队列40pts求调

P1168 中位数

Epi4any @ 2022-11-17 22:03:44

Wrong Answer on test #1 #2 #5

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int n, a[maxn];
priority_queue<int, vector<int>, less<int>> p;
priority_queue<int, vector<int>, greater<int>> q;
//1234->p 567->q
int main() {
    ios :: sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin>>n;
    for(int i=1,x;i<=n;i++) {
        cin>>x;
        if(p.empty()) p.push(x);
        else if(q.empty()) q.push(x);
        else if(x<q.top()) p.push(x);
        else if(x>=q.top()) q.push(x);
        if(q.size()>p.size()) p.push(q.top()),q.pop();
        if(p.size()>q.size()+1) q.push(p.top()),p.pop();
        if(i&1) cout<<p.top()<<endl;
    }
    return 0;
}

大佬轻喷qwq


|