pyz51 @ 2024-07-07 17:29:35
提交记录
#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;
}
感谢大佬们
by pyz51 @ 2024-07-07 18:35:00
看了别人的求助和大佬的回答,这题会了。
讨论及回答