每日一题 Running Median

https://ac.nowcoder.com/acm/problem/50940
思路:维护个对顶堆就行了。

#include<bits/stdc++.h>
//#pragma GCC optimize ("O2")
#define x first
#define y second
#define lson u<<1
#define rson u<<1|1
#define pb push_back
#define pu pushup
#define mk make_pair
using namespace std;
#define ll long long
#define debug puts("----");
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
#define ios ios::sync_with_stdio(0);//小根堆的所有值都比大根堆高 
int main()
{    
    int t;
    t=read();
    while(t--)
    {
        int cas,n;
        scanf("%d%d",&cas,&n);
        printf("%d %d\n",cas,n/2+1);

        priority_queue<int>q;
        priority_queue<int,vector<int>,greater<int>>q2;
        int cnt=0;
        for(int i=1;i<=n;i++)
        {
            int x;
            scanf("%d",&x);
            q2.push(x);
            if(i%2)
            {
                if(q2.size()>i/2+1)
                {
                    auto tt=q2.top();
                    q.push(tt);
                    q2.pop();
                }

                if(q2.size() && q.size() && q.top()>q2.top())
                {
                    auto a=q.top();
                    auto b=q2.top();
                    q.pop();
                    q2.pop();
                    q.push(b);
                    q2.push(a);
                }
                if((cnt+1)%10==0)
                printf("%d\n",q2.top());
                else
                printf("%d ",q2.top());
                cnt++;
            }

        }

        if(cnt%10)
            printf("\n");



    }
    return 0;
}
全部评论

相关推荐

安静的鲸鱼offer...:神仙级别hr,可遇不可求,甚至他可能也是突然有感而发。只能说遇上是件幸事。
秋招开始捡漏了吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务