对顶堆 样例全过,全部WA。QAQ(带注释)
  • 板块P1168 中位数
  • 楼主pyz51
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/7/7 17:29
  • 上次更新2024/7/7 20:03:01
查看原帖
对顶堆 样例全过,全部WA。QAQ(带注释)
1188369
pyz51楼主2024/7/7 17:29

提交记录

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
const int M=2e5+5;
const int inf=2147483647;
const long long lof=9223372036854775807;
#define ll long long
#define bug cout<<"...here...\n"
#define mem(a,b) memset(a,b,sizeof a)
priority_queue<int,vector<int>,greater<int> > up;
priority_queue<int> down;
//大小根堆(有可能写反了)
int n;
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        int x;
        cin>>x;
        up.push(x);//默认放入小根堆
        while(up.size()-down.size()>1) down.push(up.top()),up.pop();//若数量不平衡,则将小根堆里的数放入大根堆(默认中位数在小根堆)
        if(i%2==1) cout<<up.top()<<'\n';//若i是奇数,输出中位数
    }
    return 0;
}

感谢大佬们

2024/7/7 17:29
加载中...