蒟蒻第一天学c艹艹,手写堆36分,求助,玄关
查看原帖
蒟蒻第一天学c艹艹,手写堆36分,求助,玄关
705296
miffy_123楼主2023/12/17 20:15
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 2147483646
#define inf 9223372036854775806
#define rep(i,o,p) for(int (i)=(o);(i)<=(p);(i)++)
#define all(c) (c).begin(),(c).end()
#define per(i,o,p) for(int (i)=(o);(i)>=(p);(i)--)
#define pl putchar('\n');
int read(){
	int f=1,k=0;
	char c;
	c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-'){
			f=-1;
		}
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		k=(k<<1)+(k<<3)+(c^48);
		c=getchar();
	}
	return f*k;
}
void write(int x){
	if(x<0){
		putchar('-');
		x=-x;
	}
	if(x>9){
		write(x/10);
	}
	putchar(x%10+'0');
}
class heap{
	public:
		int h[1000],len=0;
		void init(int n=INF){
			h[0]=n;
			return;
		}
		void push(int x){
			len++;
			h[len]=x;
			int k=len;
			while(k>0&&h[k/2]>h[k]){
				swap(h[k/2],h[k]);
				k/=2;
			}
			return;
		}
		int ontop(){
			return h[1];
		}
		void erase(){
			h[1]=h[len];
			len--;
			int pa=1;
			while(pa<=len/2){
				int son=0;
				if(h[pa*2]>h[pa*2+1]){
					son=pa*2+1;
				}
				else{
					son=pa*2;
				}
				if(h[pa]<=h[son]){
					return;
				}
				else{
					swap(h[pa],h[son]);
				}
			}
			return;
		}
	private:
};
heap a;
int main(){
	int n=read();
	while(n--){
		int op=read();
		if(op==1){
			int x=read();
			a.push(x);
		}
		else if(op==2){
			write(a.ontop());
			pl
		}
		else{
			a.erase();
		}
	}
    return 0;
}
2023/12/17 20:15
加载中...