其他分享
首页 > 其他分享> > NC50940 Running Median

NC50940 Running Median

作者:互联网

题目

1.题目大意

2.题目分析

3.题目代码

#include <bits/stdc++.h>

using namespace std;

int fr(){
    char ch;
    int sum,sign=1;
    while((ch=getchar())>'9'||ch<'0') if(ch=='-') sign=-1;sum=ch-'0';
    while((ch=getchar())>='0'&&ch<='9') sum=(sum<<3)+(sum<<1)+ch-'0';
    return sum*sign;
}

int main() {
    int t;
    cin >> t;
    while(t--) {
        int num, n;
        cin  >> num >> n;
        cout << num << ' ' << n/2+1 << endl;
        priority_queue<int> gl;
        priority_queue<int, vector<int>, greater<int>> lg;
        cin >> num, lg.push(num);
        cout << num << ' ';
        // l 3 g 4 | g 2 l 1
        for(int i=2;i<=n;i++) {
            num = fr();
            if(lg.top()>num) gl.push(num);
            else lg.push(num);
            if(lg.size()>gl.size()+1) gl.push(lg.top()), lg.pop();
            else if(gl.size()>lg.size()+1) lg.push(gl.top()), gl.pop();
            if(i%2==1) 
                if(lg.size()>gl.size()) cout << lg.top() << ' ';
                else cout << gl.top() << ' ';
            if(i%20==0) cout << endl;   
        }
        cout << endl;
    }
}

标签:lg,NC50940,Median,Running,num,push,题目,gl,size
来源: https://www.cnblogs.com/zhangyi101/p/16629758.html