社区讨论

大小堆0pts求调

P1168中位数参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@m6q8ku5v
此快照首次捕获于
2025/02/04 16:47
去年
此快照最后确认于
2025/11/04 10:00
4 个月前
查看原帖
样例过但是0分 大佬有时间帮帮蒻暨吧
CPP
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;


const int INF = 1e9;


void solve()
{
    int n;cin>>n;
    vector<int>q(n + 1);
    for(int i = 1; i <= n; i ++)
        cin>>q[i];
    priority_queue<int>les;
    priority_queue<int,vector<int>,greater<int>>gr;
    auto weihu = [&les,&gr](){
        while(les.size() != gr.size() + 1)
        {
            if(les.size() > gr.size() + 1)
            {
                gr.push(les.top());
                les.pop();
            }
            else
            {
                les.push(gr.top());
                gr.pop();
            }
        }
        while(!les.empty() && les.top() < gr.top())
        {
            int t = gr.top();
            gr.pop();
            gr.push(les.top());
            les.pop();
            les.push(t);
        }
    };
    gr.push(q[1]);
    cout << q[1] << endl;
    for(int i = 2; i <= n; i ++)
    {
        gr.push(q[i]);
        if(i%2)
        {
            weihu();
            cout << gr.top() << endl;
        }
            
    }
    
}


int main()
{
    ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
    int T = 1;
    //cin>>T;
    while(T --)
    {
        solve();
    }
    return 0;
}

回复

0 条回复,欢迎继续交流。

正在加载回复...