大小堆,棧,隊列,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;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章