社区讨论

TLE求助【悬关】

SP16254RMID2 - Running Median Again参与者 2已保存回复 9

讨论操作

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

当前回复
9 条
当前快照
1 份
快照标识符
@lo1bzs2f
此快照首次捕获于
2023/10/22 18:33
2 年前
此快照最后确认于
2023/11/02 18:55
2 年前
查看原帖
RT
CPP
#include<bits/stdc++.h>
using namespace std;
int t,n;
int main() {
    cin >> t;
    while(t--) {
        priority_queue<int,vector<int>,less<int>>q1;//大根堆
        priority_queue<int,vector<int>,greater<int>>q2;//小根堆     
        while(scanf("%d",&n)){
            if (n==0) break;
            if (n==-1) {
                if ((q1.size()+q2.size())%2==0) {
                    cout << q1.top() << endl;
                    q1.pop();
                }
                else {
                    cout << q2.top() << endl;
                    q2.pop();
                }
            }
            else {
                if (q2.size()==0||n>q2.top()) q2.push(n);
                else q1.push(n);
            }
            while(q1.size()>q2.size()) q2.push(q1.top()),q1.pop();
            while(q2.size()>q1.size()+1) q1.push(q2.top()),q2.pop();
        }
    }
    return 0;
}

回复

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

正在加载回复...