STL-priority_queue优先队列容器简单用法(POJ 3253 Fence Repair(哈夫曼树))

priority_queue优先队列容器与队列一样,C++头文件需要#include<queue>,只能从队尾插入元素,从队首删除元素,但是它有一个特性,就是队列中最大的元素总是位于队首,并且按先进先出的原则进行。优先队列的好处就是:元素的比较规则默认为按元素的由大到小排序,不过可以重载“<”重新定义比较规则。

优先队列包括入队push()(插入元素),出队pop()(删除元素),读取队首元素top(),判断队列是否为空empty()和读取队列元素的数量size()等方法。

定义队列等操作我就不多说了,操作跟我之前谈map容器相似(https://blog.csdn.net/qq_42391248/article/details/81041669

重载“ < ”:

//这是定义一个结构体在优先队列中排序规则 
struct FFF{
	int m,n,p,q;
	bool operator < (const FFF &a) const
	{
		return a.q<q;//按q从小到大排列 
	}
};
priority_queue<FFF> mmm;

应用: https://blog.csdn.net/qq_42391248/article/details/81669489

重载“()”:

//使元素进队从小到大排序
struct mycomp
{
	bool operator()(const int &a,const int &b)
	{
		return a>b;
	}
};
//在主函数里定优先队列时
priority_queue<int,vector<int>,mycomp> s;
或者直接定义:
priority_queue<int ,vector<int>,greater<int> >s;

优先队列用于模拟哈夫曼树的题很简单。

下面说一道用到这个stl很简单的题(哈夫曼树):

Fence Repair

Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the "kerf", the extra length lost to sawdust when a sawcut is made; you should ignore it, too.

FJ sadly realizes that he doesn't own a saw with which to cut the wood, so he mosies over to Farmer Don's Farm with this long board and politely asks if he may borrow a saw.

Farmer Don, a closet capitalist, doesn't lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.

Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.

Input

Line 1: One integer N, the number of planks
Lines 2.. N+1: Each line contains a single integer describing the length of a needed plank

Output

Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts

Sample Input

3
8
5
8

Sample Output

34

这道题就是求二叉树的最小权值。典型的哈夫曼树问题(最优二叉树)。

大体思路这样的:把所有数据按升序排列,选出最小的两个数据相加后删除出数组,记录权值,把相加后得到的数插入数组再排序,进行相同的操作。最后把所有权值加一起,就是最小的权值。

如果用循环的模拟过程的话,比如会超时,所有就需要用到非常非常好用的优先队列了。

具体代码如下:

#include<iostream>
#include<queue>
using namespace std;
struct mycomp
{
	bool operator()(const int &a,const int &b)
	{
		return a>b;
	}
};
int main()
{
	int n;
	int a[30010];
	cin>>n;
	long long sum=0;
	priority_queue<int,vector<int>,mycomp> s;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		s.push(a[i]);
	}
	while(s.size()>1)
	{
		int a,b;
		a=s.top();
		s.pop();
		b=s.top();
		s.pop();
		sum+=a+b;
		s.push(a+b);	
	}		
	printf("%lld\n",sum);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wonder-King

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值