2017计算机学科夏令营上机考试G:实现堆结构(堆+优先队列)

在这里插入图片描述
在这里插入图片描述

思路分析

方法一: 利用全局数组实现heap。需要熟练掌握堆的结点删除、插入,删除利用 downAdjust(low, high) 函数,插入利用 updownAdjust(low, high) 函数。
注意点:

  1. heap数组下标要从1开始,否则无法计算左右孩子结点的下标;
  2. 入堆向上调整,出堆向下调整,建堆从右向左、从下往上调整(逆序for循环+向下调整);
  3. 堆排序先建堆,从右向左(不包含堆顶结点),一边与堆顶结点交换,一边向下调整。

方法二: 使用优先队列priority_queue。优先队列是用堆实现的,所以优先队列中的push()、pop()操作的时间复杂度都是O(nlogn)。

优先队列的初始化需要三个参数,元素类型容器类型比较算子

需要熟悉的优先队列操作:

  1. q.top() 访问堆顶
  2. q.push() 入堆
  3. q.pop() 出堆
  4. 不同类型元素的优先级设置
  5. 定义堆需要注意最后两个>_>之间有一个空格

1. 基本数据类型:

priority_queue < int, vector<int>, less<int> > q; // 大顶堆——堆顶为大数
priority_queue < int, vector<int>, greater<int> > q; // 小顶堆——堆顶为小数

2. 结构体类型:

/*方法一:友元函数、运算符重载*/
struct fruit
{
	string name;
	int price;
	friend bool operator < (fruit f1, fruit f2)
	{
		return f1.price < f2.price; // 大顶堆——堆顶为大数
	}
};
priority_queue<fruit> q;

struct fruit
{
	string name;
	int price;
	friend bool operator < (fruit f1, fruit f2)
	{
		return f1.price > f2.price; // 小顶堆——堆顶为小数
	}
};
priority_queue<fruit> q;
/*方法二:cmp函数*/
struct fruit
{
	string name;
	int price;
};

struct cmp
{
	bool operator () (fruit f1, fruit f2)
	{
		return f1.price < f2.price; // 大顶堆——堆顶为大数
	}
};
priority_queue<fruit, vector<fruit>, cmp> q;

struct cmp
{
	bool operator () (fruit f1, fruit f2)
	{
		return f1.price > f2.price; // 小顶堆——堆顶为小数
	}
};
priority_queue<fruit, vector<fruit>, cmp> q;

代码——方法一

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

const int maxn = 100010;
int heap[maxn] = {0};

// 对heap在[low, high]范围内进行调整
void downAdjust(int low, int high) // 向下调整
{
    int i = low; // i为待调整接点
    int j = 2 * i; // j为其左孩子
    while(j <= high) // 左孩子存在
    {
        if(j+1<=high && heap[j+1]<heap[i]) // 右孩子存在,且小于父亲结点
        {
            j = j + 1; // 使j保持为最大的那个孩子结点
        }
        if(heap[j] < heap[i]) // 小顶堆 孩子结点<父亲结点 时进行调整
        {
            swap(heap[j], heap[i]);
            i = j; 
            j = 2 * i;
        }
        else 
        {
            break;
        }
    }
}

void upAdjust(int low, int high) // 向上调整
{
    int i = high; // i为待调整结点
    int j = i / 2; // j为其父亲结点
    while(j >= low) // 父亲结点存在
    {
        if(heap[j] > heap[i]) // 小顶堆 父亲结点>孩子结点 时进行调整
        {
            swap(heap[j], heap[i]);
            i = j;
            j = i / 2;
        }
        else
        {
            break;
        }
    }
}

/*
// 建堆
void createHeap(int n)
{
    for(int i=n/2; i>=1; i--)
    {
        downAdjust(i, n);
    }
}

// 堆排序
void heapSort(int n)
{
    createHeap(n);
    for(int i=n; i>1; i++)
    {
        swap(heap[i], heap[1]);
        downAdjust(1, i-1);
    }
}
*/

// 删除堆顶结点
void deleteHeap(int &n) // 一定要加引用才能改变实参
{
    printf("%d\n", heap[1]);
    heap[1] = heap[n--]; // 最后一个结点覆盖堆顶结点,长度-1
    downAdjust(1, n); // 向下调整堆顶元素
}

// 插入新结点
void insertHeap(int &n, int x)
{
    heap[++n] = x; // 数组末尾赋值x,长度+1
    upAdjust(1, n); // 向上调整新加入的结点
}

int main()
{
    // freopen("input.txt", "r", stdin);
    int t;
    scanf("%d", &t);
    for(int i=0; i<t; i++)
    {
        int n, type;
        scanf("%d", &n);
        int cnt = 0; // 记录堆结点个数
        for(int j=0; j<n; j++)
        {
            scanf("%d", &type);
            if(type == 1)
            {
                int u;
                scanf("%d", &u);
                insertHeap(cnt, u); // 插入结点
            }
            if(type == 2)
            {
                deleteHeap(cnt); // 删除堆顶结点
            }
        }
    }
    // fclose(stdin);

    return 0;
}

代码——方法二

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

priority_queue<int, vector<int>, greater<int> > q; // 小堆顶结构

int main()
{
    // freopen("input.txt", "r", stdin);
    int t;
    scanf("%d", &t);
    for(int i=0; i<t; i++)
    {
        int n, type;
        scanf("%d", &n);
        for(int j=0; j<n; j++)
        {
            scanf("%d", &type);
            if(type == 1)
            {
                int u;
                scanf("%d", &u);
                q.push(u);
            }
            if(type == 2)
            {
                printf("%d\n", q.top());
                q.pop();
            }
        }
    }
    // fclose(stdin);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值