手写堆求调!!!
查看原帖
手写堆求调!!!
989997
DGL__DGL_AFO楼主2024/4/11 21:29

A了1 2 3 11

#include<bits/stdc++.h>
using namespace std;
long long x,a;
int op;
long long tr[1114514],cnt;

int main()
{
	//ios::sync_with_stdio(0),cin.tie(0);
	cin>>x;
	memset(tr,0x3f,sizeof tr);
	tr[0]=-1e9;
	while(x--)
  {
  	cin>>op;
  	if(op==1)
    {
    	cnt++;
    	cin>>tr[cnt];
    	int j;
    	j=cnt;
    	while(1)
    	{
    		if(tr[j>>1]>tr[j])
    		{
    			//cout<<j/2<<' '<<j<<" "<<tr[j>>1]<<' '<<tr[j]<<endl;
    			a=tr[j>>1];
    			tr[j>>1]=tr[j];
    			tr[j]=a;
    			//res=j;
    			j=j>>1;
				}
				else
				  break;		  
			}
			//cout<<tr[1]<<endl;	
		}
		else if(op==2)
		{
			cout<<tr[1]<<endl;
		}	
		else
		{
			tr[1]=tr[cnt];
			tr[cnt]=1e9;
			cnt--;
			int j;
			if(tr[2]<=tr[3])
			  j=2;
			else
			  j=3; 
			int res=1;	 
			while(1)
			{
				a=tr[res];//cout<<res<<' '<<j<<endl;
				tr[res]=tr[j];
				tr[j]=a;
				res=j;
				if(tr[j<<1]<=tr[j<<1|1]&&tr[j<<1]<tr[res])
				  j=j<<1;
				else if(tr[j<<1|1]<tr[j<<1]&&tr[j<<1|1]<tr[res])
				  j=j<<1|1;
				else
				  break;
				if(j>cnt) break;				  
			}
			//cout<<cnt<<' '<<tr[1]<<endl;
		}
		
	}
  	
	
	
	return 0;
} 
2024/4/11 21:29
加载中...