动态求中位数

题目详情 - L3-002 特殊堆栈 (30 分) (pintia.cn)

Vector

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>

using namespace std;

int n, x;
string op;
vector<int> res; 
stack<int> st;
    
void Push(int x)
{
    auto pos = lower_bound(res.begin(), res.end(), x);
    res.insert(pos, x);
}

int Pop(int x)
{
    auto pos = lower_bound(res.begin(), res.end(), x);
    res.erase(pos);
}

int Get()
{
    if(res.size() & 1)  return res[(res.size() + 1) / 2 - 1];
    return res[res.size() / 2 - 1];
}

int main()
{
    // ios::sync_with_stdio(false);
    
    cin >> n;
    for(int i = 0; i < n; i ++ )
    {
        cin >> op;
        if(op == "Pop")
        {
            if(!st.size()) puts("Invalid");
            else    
            {
                cout << st.top() << endl;
                Pop(st.top());
                st.pop();
            }
        }
        else if(op == "PeekMedian")
        {
            if(!st.size()) puts("Invalid");
            else cout << Get() << endl;
        }
        else if(op == "Push")
        {
            int x;  cin >> x;
            st.push(x);
            Push(x);
        }
    }
    
    
    return 0;
}

对顶堆

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>

using namespace std;

int n, x;
string op;
vector<int> res; 
stack<int> st;
    
void Push(int x)
{
    auto pos = lower_bound(res.begin(), res.end(), x);
    res.insert(pos, x);
}

int Pop(int x)
{
    auto pos = lower_bound(res.begin(), res.end(), x);
    res.erase(pos);
}

int Get()
{
    if(res.size() & 1)  return res[(res.size() + 1) / 2 - 1];
    return res[res.size() / 2 - 1];
}

int main()
{
    ios::sync_with_stdio(false);
    cin >> n;
    
    for(int i = 0; i < n; i ++ )
    {
        cin >> op;
        if(op == "Pop")
        {
            if(!st.size())  puts("Invalid"); 
                //cout << "Invalid" << endl;
            else    
            {
                cout << st.top() << endl;
                Pop(st.top());
                st.pop();
            }
        }
        else if(op == "PeekMedian")
        {
            if(!st.size())  puts("Invalid"); 
                //cout << "Invalid" << endl;
            else cout << Get() << endl;
        }
        else if(op == "Push")
        {
            int x;  cin >> x;
            st.push(x);
            Push(x);
        }
    }
    
    
    return 0;
}

树状数组

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 100010;

int s[N], top;//top指向要插入数据的位置
int tr[N];

int lowbit(int x)
{
    return x & -x;
}

void update(int x, int v)
{
    for(int i = x; i < N; i += lowbit(i))
        tr[i] += v;
}

int getsum(int x)
{
    int sum = 0;
    for(int i = x; i ; i -= lowbit(i))
        sum += tr[i];
    return sum;
}

int query(int x)
{
    if(x % 2)   x = (x + 1) / 2;
    else    x /= 2; //x=中位数的位置
    
    int l = 1, r = N - 1;
    while(l < r)
    {
        int mid = (l + r) >> 1;
        if(getsum(mid) >= x)    r = mid;
        else    l = mid + 1;
    }
    return l;
}

int main()
{
    ios::sync_with_stdio(false);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    int n;  cin >> n;
    while(n -- )
    {
        string str;   cin >> str;
        if(str == "Pop")
        {
            if(!top)    cout << "Invalid" << endl;
            else
            {
                top -- ;
                update(s[top], -1);
                cout << s[top] << endl;
            }
        }
        else if(str == "PeekMedian")
        {
            if(!top)    cout << "Invalid" << endl;
            else
            {
                int x = top;
                cout << query(x) << endl;
            }
        }

        else
        {
            int x;  cin >> x;
            s[top ++ ] = x;
            update(x, 1);
        }
    }
    return 0;
}

参考大佬博客

(7条消息) L3-002 特殊堆栈 (30分)(四种方法)_qq_45778406的博客-CSDN博客

posted @ 2022-05-05 08:41  光風霽月  阅读(35)  评论(0编辑  收藏  举报