TLE28求调
查看原帖
TLE28求调
1034459
NumsLock楼主2024/10/17 22:14
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int h[N],cnt,n,ls;
void up(int p){
	while(p>1){
		if(h[p]<h[p/2]){
			swap(h[p],h[p/2]);
			p/=2;
		}else{
			break;
		}
	}
	return;
}void down(int p){
	while(p*2<cnt){
		if(p*2+1<cnt){
			if(h[p]<min(h[2*p],h[2*p+1])){
				break;
			}else{
				if(h[2*p]<h[2*p+1]){
					swap(h[p],h[2*p]);
					p*=2;
				}else{
					swap(h[p],h[2*p+1]);
					p=p*2+1;
				}
			}
		}else{
			if(h[p]>h[2*p]){
				swap(h[p],h[2*p]);
			}
		}
	}
	return;
}void pop(){
	h[1]=h[cnt];
	cnt--;
	down(1);
}void insert(int p){
	cnt++;
	h[cnt]=p;
	up(cnt);
}int main(){
	cin>>n;
	while(n--){
		cin>>ls;
		if(ls==1){
			cin>>ls;
			insert(ls);
		}else if(ls==2){
			cout<<h[1]<<endl;
		}else if(cnt>1){
			pop();
		}
	}
	return 0;
}
2024/10/17 22:14
加载中...