大小堆,栈,队列,vector点点滴滴的总结

洛谷1090经典贪心例题

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
**priority_queue<int,vector<
 int> ,greater<int> > q;**//c++内置stl的排序按top为min 同理less
int n,num;
int a[10100];
int main(int argc, char** argv) {
 cin>>n;
 for(int i=1;i<=n;i++)
 {cin>>a[i];q.push(a[i]);}
 while(q.size()>2){
 int t=0;
 t=q.top();q.pop();
 t+=q.top();q.pop();
 q.push(t);
 num+=t;
 t=0;
}
 num+=q.top();
 q.pop();
 cout<<num+q.top()<<endl;
 fclose(stdin);fclose(stdout);
 return 0;
}
//![在这里插入图片描述](https://

img-blog.csdnimg.cn/2019092420272485.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwNDkzODI5,size_16,color_FFFFFF,t_70)

以前的堆

priority_queue<int,vector<int>,less> q;

只能一个一个放数
加上重载运算符是不是可以放数组或其他的东西呢???
我们知道小堆是靠bool判断的
所以
只要重载下bool

struct cmp
{
			bool  operator()(int x,int y)
			{
				return dis[x]>dis[y] ;//大根堆头最小
			}
			};


//写

文章自动忽略空格?
堆变成

priority_queue<int,vector<int>,cmp> q;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值