40pts求助谢谢

P1168 中位数

SESpy @ 2024-07-18 18:00:31

rt

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,x;
    set<int> a,b;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>x;
        if(b.size()==0)
            b.insert(x);
        else if(a.size()!=0)
        {
            if(x<=*a.rbegin())
                a.insert(x);
            else
                b.insert(x);
        }
        else
            b.insert(x);
        if(a.size()>b.size())
        {
            b.insert(*a.rbegin());
            a.erase(*a.rbegin());
        }
        else if(b.size()-a.size()>1)
        {
            a.insert(*b.begin());
            b.erase(*b.begin());
        }
        if(i%2)
        {
            cout<<*b.begin()<<endl;
        }
    }
    return 0;
}

by 哈哈人生 @ 2024-07-22 15:28:42

  1. n^2会T
  2. set去重会WA

by 哈哈人生 @ 2024-07-24 14:22:47

@SESpy


by SESpy @ 2024-07-25 10:22:55

@哈哈人生 好 谢谢


|