1,堆排序 (输入一个长度为n的整数数列,从小到大输出前m小的数)

      包含 down 操作

//堆排序
#include<iostream>
using namespace std;
const int N=100010;
int n,m;
int h[N],size;

void down(int u)
{
	int t=u;
	if(u*2<=size && h[2*u]<h[t]) t=2*u;
	if(2*u+1<=size && h[2*u+1]<h[t]) t=2*u+1;
	if(t!=u)
	{
		swap(h[u],h[t]);
		down(t);
	}
}

int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>h[i];
	size=n;
	for(int i=n/2; i ;i--) down(i);
	while(m--)
	{
		cout<<h[1]<<" ";
		h[1]=h[size];
		size--;
		down(1);	
	}
	return 0;
}

2,模拟堆

维护一个集合,初始时集合为空,支持如下几种操作:
1.“ I x”,插入一个数x;
2."PM",输出当前集合中的最小值;
3.“DM”,荆除当前集合中的最小值(当最小值不唯一时,删除最早插入的最小体)
4.“D k” 删除第k个插入的数;
5."C k x”,修改第k个插入的数,将其变为x;
现在要进行N次操作,对于所有第2个操作,输出当前集合的最小值

//模拟堆
#include<iostream> 
#include<algorithm>
#include<string.h>
using namespace std;
const int N=100010;
int h[N],ph[N],hp[N],size;

void heap_swap(int a,int b)
{
	swap(ph[hp[a]],ph[hp[b]]);
	swap(hp[a],hp[b]);
	swap(h[a],h[b]);
}

void down(int u)
{
	int t=u;
	if(2*u<=size && h[2*u]<h[t]) t=2*u;
	if(2*u+1<=size && h[2*u+1]<h[t]) t=2*u+1;
	if(t!=u)
	{
		heap_swap(t,u);
		down(t);
	}
}

void up(int u)
{
	while(u/2 && h[u/2]>h[u])
	{
		heap_swap(u/2,u);
		u/=2;
	}
}

int main()
{
	int n,m=0;
	cin>>n;
	while(n--)
	{
		char op[10];
		int k,x;
		scanf("%s",op);
		if(!strcmp(op,"I"))
		{
			cin>>x;
			size++;
			m++;
			ph[m]=size;
			hp[size]=m;
			h[size]=x;
			up(size);
		}
		else if(!strcmp(op,"PM"))
		{
			cout<<h[1]<<endl;
		}
				
		else if(!strcmp(op,"DM"))
		{
			heap_swap(1,size);
			size--;
			down(1);
		}
		else if(!strcmp(op,"D"))
		{
			cin>>k;
			k=ph[k];
			heap_swap(k,size);
			size--;
			down(k),up(k);
		}
		else
		{
			cin>>k>>x;
			k=ph[k];
			h[k]=x;
			down(k),up(k);
		}
	}
	return 0;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值