优先队列的自定义排序


优先队列的自定义排序

优先队列

那么何为优先队列呢,在优先队列中,元素被赋予优先级,当访问元素时,具有最高级优先级的元素先被访问(即优先队列具有最高级先出的行为特征)
优先队列在头文件#include<queue>中;
其声明格式为:priority_queue<int>ans; //声明一个名为ans的整型的优先队列
基本操作有:
empty( ) //判断一个队列是否为空
pop( ) //删除队顶元素
top( ) //返回优先队列的队顶元素
push( ) //加入一个元素
size( ) //返回优先队列中拥有的元素个数
优先队列的时间复杂度为O(logn),n为队列中元素的个数,其存取都需要时间。

第一种用法-默认从大到小排序

#include<iostream>
#include<queue>
using namespace std;
priority_queue<int> q1;//默认从大到小排序 
int main()
{
	int n;
	cin>>n;
	int t;
	for(int i=1;i<=n;i++)
	{
		cin>>t;
		q1.push(t);
	}
	while(!q1.empty())
	{
		cout<<q1.top()<<" ";
		q1.pop();
	}
	return 0;
}

输入:6  
4 8 2 9 5 7   
输出:
9 8 7 5 4 2

第二种用法-从小到大排序


#include<iostream>
#include<queue>
using namespace std;
priority_queue<int,vector<int>,greater<int> >q1;//从大到小排序,最后两个> >中间必须要用空格 
int main()
{
	int n;
	cin>>n;
	int t;
	for(int i=1;i<=n;i++)
	{
		cin>>t;
		q1.push(t);
	}
	while(!q1.empty())
	{
		cout<<q1.top()<<" ";
		q1.pop();
	}
	return 0;
}

6
4 8 2 9 5 7
2 4 5 7 8 9

第三种用法-自定义排序规则

#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int tmp[100];
struct cmp1{
	bool operator()(int x,int y)
	{
		return x>y;   //小的优先级高 ,从小到大排 
	}
}; 

struct node{
	int x,y;
	friend bool operator<(node a,node b)   //自定义存储类型的时候就定义好排序规则
	{
		return a.y>b.y;   //按y从小到大排 
	}
};
priority_queue<int>q1;
priority_queue<int,vector<int>,cmp1>q2;
priority_queue<node>q4;
int main()
{
	int i,j,k,m,n;
	int x,y;
	node a;
	while(cin>>n)
	{
		for(int i=0;i<n;i++)
		{
			cin>>a.x>>a.y;
			q4.push(a);
		}
		cout<<endl;
		while(!q4.empty())
		{
			cout<<q4.top().y<<" "<<q4.top().x<<" "<<endl;
			q4.pop();
		}
		cout<<endl;
		
	int t;
		for(i=0;i<n;i++)
		{
			cin>>t;
			q2.push(t);
		}
		while(!q2.empty())
		{
			cout<<q2.top()<<" ";
			q2.pop();
		}
		cout<<endl;
	}
	return 0;
}

5          4 8 2 5 9
7 8        2 4 5 8 9
6 5
3 9
9 4
3 7

9 4
6 5
3 7
7 8
3 9

实例-LeetCode 5703最大平均通过率

class Solution {
public:
        struct cmp {//自定义排序规则 优先队列用
        bool operator() (pair<int,int> &a, pair<int,int> &b) {//小顶堆,按照Node对象的id升序排列
            if (a.first == a.second) return true;
            if (b.first == b.second) return false;
            double t1=(((double)a.first + (double)1.0) /((double)a.second + (double)1.0))-((double)a.first/(double)a.second);
            double t2=(((double)b.first + (double)1.0) /((double)b.second + (double)1.0))-((double)b.first/(double)b.second);
            return t1 < t2;//按照增量降序
        }
    };
    
    double maxAverageRatio(vector<vector<int>>& classes, int extraStudents) {
        priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> my_pq; //定义一个以pair<int, int>为基量的优先队列
        int n = classes.size();
        for (int i = 0; i < n; i++) {
            pair<int, int> p(classes[i][0], classes[i][1]);
            my_pq.push(p);
        }
        while (extraStudents != 0) {
            pair<int, int>p = my_pq.top(); my_pq.pop();
            p.first += 1;
            p.second += 1;
            my_pq.push(p);
            extraStudents -= 1;
        }
        double res = 0;
        while (!my_pq.empty()) {
            pair<int,int>p= my_pq.top(); my_pq.pop();
            double d = ((double)p.first / (double)p.second);
            res += d;
        }
        return res/n;
    }
};

详细内容可以去我的个人博客观看,请戳这里

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值