优先队列的使用

1 篇文章 0 订阅
0 篇文章 0 订阅

#优先队列
优先队列就是STL的#include<queue> 里面的 priority_queue,它的原理就是通过堆,这里来一张书上的图
这里写图片描述
也就是每push() 一个元素,这个priority_queue就会自动排序,也就是通过堆排序中的调整堆,复杂度也是相当好的O(logn)
使用的时候要注意,如果是基础类型,它默认是降序所以要想升序有点小改动,而且对于基础类型,库函数中提供了可以直接调用的函数
升序 : priority_queue<int, vector<int>, greater<int> > list;
降序 : priority_queue<int, vector<int>, less<int> > list;
其中要注意的是最后那两个 > 不能贴在一起,因为就和 >> 冲突了
但是如果遇到自己想排序的结构体或者class的时候,就只有自己动手丰衣足食了。。。

#include<iostream>
#include<queue>
using namespace std;
typedef struct Node{
	int x, y;
	Node(int xx, int yy){x = xx; y = yy;}
	bool operator < (Node const &a) const {
		return x > a.x;
	}
}Node;

int main(){
	priority_queue<Node> list;
	list.push(Node(5,3)); 
	list.push(Node(1,1)); 
	list.push(Node(3,3));
	while(!list.empty()){
		Node temp = list.top();
		list.pop();
		cout <<  "(" << temp.x << " " << temp.y << ") ";
	}
	return 0;
}

这里写图片描述

这里有几点要注意的地方:
1 、重载运算符的时候只能重载 < 符号
2 、申明方法参数只有一个,而且是const 引用类型
3 、方法参数后面要加一个 const ,这是表明此方法不能修改类中的成员变量

第一题poj3253
这道题遇到过,就是哈夫曼树的思想,不停找当前数组中最小的两个,然后将它们拿出来然后相加,再讲相加的和又放入到数组中,我之前就用学校头学的哈夫曼树的方法切套这道题还超时了。。。
这里就用priority_queue 来解决这道题

#include<iostream>
#include<queue>
using namespace std;
int length;

int main(){
	priority_queue<long long, vector<long long>, greater<long long> > list;
	long long input;
	cin >> length;
	for(int i = 0; i < length; i++){
		cin >> input;
		list.push(input);
	}
	
	long long a, b, answer = 0;
	while(list.size() > 1){
		a = list.top(); list.pop();
		b = list.top(); list.pop();
		answer += a + b;
		list.push(a + b);
	}
	
	cout << answer << endl;
	return 0;
}

第二题poj2431
这道题还是出了很多问题,我开始想的是不停的找从当前位置向目的地走,其中找出最大的那个可以加油的加油站,然后下一次就直接定位到这个加油站位置,又继续那个过程,但是紧到WA,我后头突然想到一个测试数据
10
1 0
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 5
10 4
11 3
对于这组数据来说,如果还是按我刚才那个想法就有问题了,因为他每次都切找最大的,那他第一次肯定是找9这个位置的加油站,然后下一次的时候就从9这个位置开始继续往前找,然后前面都是1了,但其实应该在 10 这个位置也停下加油,然后就可以直接到目的地了,所以我就把找过的所有都放到一个优先队列里面,然后如果可以到更前面的加油站时,我就又把新的加油站放进这个优先队列中,然后不停找优先队列中最大的那个

#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
typedef struct Node{
	int index;
	int dist, fuel;
	Node(){}
	bool operator < (Node const &a) const {
		return fuel < a.fuel;
	}
}Node;
Node shuzu[10005];
int dist, fuel;
int jishu;

bool cmp(Node a, Node b){
	return a.dist < b.dist;
}

int main(){
	cin >> jishu;
	for(int i = 0; i < jishu; i++) 
		 cin >> shuzu[i].dist >> shuzu[i].fuel;
	sort(shuzu, shuzu+jishu, cmp);
	for(int i = 0; i < jishu; i++) shuzu[i].index = i;
	
	cin >> dist >> fuel;
	int i = jishu - 1;
	int answer = 0;
	int flag = 1;
	priority_queue<Node> list;		
	while(dist-fuel > 0){		
		for(; i>=0 && shuzu[i].dist>=dist-fuel; i--){			
			if(shuzu[i].index == -1) continue;
			list.push(shuzu[i]);
		}
		
		if(list.empty()) {flag = 0; break;}
		Node temp = list.top();
		list.pop(); 
		if(dist >= temp.dist){
			fuel = fuel - dist + temp.dist + temp.fuel;
			dist = temp.dist;
		}else{
			fuel = fuel + temp.fuel;
		}				
		shuzu[temp.index].index = -1;
		answer ++;
	
	}
	
	if(flag == 1){
		cout << answer << endl;
	}else{
		cout << "-1" << endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值