P3871 [TJOI2010]中位数

傻逼题 维护两个系统堆即可

#include<bits/stdc++.h>
#define il inline
#define vd void
typedef long long ll;
il int gi(){
	int x=0,f=1;
	char ch=getchar();
	while(!isdigit(ch)){
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
	return x*f;
}
std::priority_queue<int>A,B;
il vd insert(int x){
	if(A.empty())return A.push(x);
	if(x<=A.top()){
		A.push(x);
		if(A.size()>B.size()+1)B.push(-A.top()),A.pop();
	}else{
		B.push(-x);
		if(B.size()>A.size())A.push(-B.top()),B.pop();
	}
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("3871.in","r",stdin);
	freopen("3871.out","w",stdout);
#endif
	int n=gi();
	for(int i=1;i<=n;++i)insert(gi());
	char opt[6];int m=gi();
	while(m--){
		scanf("%s",opt);
		if(opt[0]=='a')insert(gi());
		else printf("%d\n",A.top());
	}
	return 0;
}
posted @ 2018-10-04 20:49  菜狗xzz  阅读(155)  评论(0编辑  收藏  举报