中位数

用stl的快速(On^2)来过数据不是特别强的Onlgn的方法

 

用优先队列实现的大根堆、小根堆来做这道题

#include<bits/stdc++.h>
using namespace std;
int n,tmp,ans;
priority_queue<int, vector<int>, greater<int> > mi;//存储更大的部分
priority_queue<int> ma;//存储更小的部分
int main()
{
    int n;
    scanf("%d", &n);
    int x;
    for(int i = 1; i <= n; i++)
    {
        scanf("%d", &x);
        if(i & 1)
        {
            if(mi.empty())
                mi.push(x);
            else
            {
                int tmp = ma.top();
                if(x > tmp)
                {
                    mi.push(x);
                }
                else
                {
                    ma.pop();
                    ma.push(x);
                    mi.push(tmp);
                }
            }
            printf("%d\n", mi.top());
        }
        else
        {
            int tmp = mi.top();
            if(x > tmp)
            {
                mi.pop();
                mi.push(x);
                ma.push(tmp);
            }
            else
            {
                ma.push(x);
            }
        }
    }
    return 0;
}
View Code

 

树状数组做法(现在还不会)

posted @ 2021-04-13 19:22  bear_xin  阅读(82)  评论(0)    收藏  举报