萌新求助可删堆
查看原帖
萌新求助可删堆
325613
Implicit楼主2020/10/17 11:15

16pts,#1,#11 AC,其他全 tle

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<queue>
#include<map>
#include<set>
using namespace std;
const int N=1e6+5;
int q,t,opt;
struct heap
{
	int n,z[N],cc; 
	heap(){n=0; cc=0;}
	int top(){return z[1];}
	inline void insert(int x)
	{
		z[++n]=x; int p=n;
		while ((p>1)&&(z[p]<z[p/2])) std::swap(z[p],z[p/2]),p/=2;
		++cc;
	}
	inline void remove()
	{
		std::swap(z[1],z[n]); --n; int p=1;
		while (2*p<=n)
		{
			int pp=2*p;
			if ((pp+1<=n)&&(z[pp+1]<z[pp])) ++pp;
			if (z[p]>z[pp]){std::swap(z[p],z[pp]); p=pp;}
		} --cc;
	}
	inline bool empty(){return !cc;}
};
const int inf=(1<<30);
struct dheap
{
    heap f,g;
    inline void insert(int v){if (v!=-inf) f.insert(v);} 
    inline void remove(int v){if (v!=-inf) g.insert(v);}
    inline int top()
    {
        while (true)
		{
            if (f.empty()) return -inf;
            if (g.empty()) return f.top();
            if (f.top()==g.top()) f.remove(),g.remove();
            else return f.top();
        }
    }
}heap;
int main()
{
	scanf("%d",&q);
	while (q--)
	{
		scanf("%d",&opt);
		if (opt==1){scanf("%d",&t); heap.insert(t);}
		else if (opt==2){printf("%d\n",heap.top());}
		else heap.remove(heap.top());
	}
	return 0;
}

/kel 为啥 tle 啊

2020/10/17 11:15
加载中...