对顶堆60pts求调
查看原帖
对顶堆60pts求调
229373
Xeqwq楼主2022/5/21 09:41
#include <cstdio>
#include <queue>
#include <string>
#include <iostream>
using namespace std;
priority_queue<int,vector<int>,less<int> > small;
priority_queue<int,vector<int>,greater<int> > big;
int main()
{
	int n;
	scanf("%d",&n);
	int x;
	string s;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&x);
		small.push(x);
	}
	int cnt=n&1;
	scanf("%d",&n);
	while(n--)
	{
		cin>>s;
		if(s=="add")
		{
			cnt^=1;
			scanf("%d",&x);
			small.push(x);
			while(cnt&1?small.size()>big.size()+1:small.size()>big.size())
			{
				big.push(small.top());
				small.pop();
			}
			while(small.top()>big.top())
			{
				small.push(big.top()),big.push(small.top());
				small.pop();big.pop();
			}
		}
		else printf("%d\n",small.top());
	}
	return 0;
}
2022/5/21 09:41
加载中...