12pts求调(通过#12)
查看原帖
12pts求调(通过#12)
1160169
lihanying2011楼主2024/9/14 18:11
//P3378 【模板】堆
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N=1e7+10;
int a[N]={0},n,tmp=1;

void fu(int f){
	if(f/2!=0 && a[f/2]>a[f]) swap(a[f],a[f/2]),fu(f/2);
}

void push(int x){
	a[++tmp]=x;
	fu(tmp);
}

void down(int m){
	int n=m;
	if(2*m<=tmp && a[2*m]<a[n]) n=m*2;
	if(2*m+1<=tmp && a[2*m+1]<a[n]) n=m*2+1;
	if(n!=m){
		swap(a[m],a[n]),down(n);
	}
}

void de(){
	a[1]=a[tmp];
	//tmp-- 加了之后能对第一个,但最后一个会错 
	down(1);
}

signed main(){
	cin>>n;
	while(n--){
		int op,x;
		cin>>op;
		if(op==1){
			cin>>x;
			push(x);
		}
		if(op==2){
			cout<<a[1]<<endl;
		}
		else de();
	}
	return 0;
}
2024/9/14 18:11
加载中...