C++優先隊列類似隊列,但是在這個數據結構中的元素按照一定的斷言排列有序。
頭文件:#include<queue>
參數:priority_queue<Type, Container, Functional>,其中Type 為數據類型,Container為保存數據的容器,默認vector,Functional 為元素比較方式,默認升序
priority_queue<Type>后邊兩個參數不加,默認為大頂堆
代碼 |
含義 |
|---|---|
p.push() |
壓入一個元素 |
p.pop() |
壓出一個元素 |
p.top() |
返回優先隊列中優先級最高的元素 |
p.empty() |
優先隊列為空,返回真 |
p.size() |
返回優先隊列中元素個數 |
基礎使用方法:
/* priority_queue的使用 */
#include<iostream>
#include<queue>
#include<vector>
#include<functional>
using namespace std;
struct nod {
int x;
int y;
nod(int a = 0, int b = 0) :x(a), y(b) {}
};
int main() {
int a[10] = { 5,1,3,8,2,4,6,9,0,7 };
/* 大頂堆 vector<int> */
cout << "\n\n大頂堆 vector<int>:\n";
priority_queue<int>p; //priority_queue<int,vector<int> > p;
for (int i = 0; i < 10; i++)p.push(a[i]);
while (!p.empty()) {
cout << p.top()<<" ";
p.pop();
}
//9 8 7 6 5 4 3 2 1 0
/* 大頂堆 pair<int,int> */
cout << "\n\n大頂堆 pair<int,int>:\n"; //先比較first,相同的話,比較second
priority_queue<pair<int, int>>p1;
pair<int, int>a1(1, 2);
pair<int, int>a2(1, 1);
pair<int, int>a3(2, 1);
p1.push(a1);
p1.push(a2);
p1.push(a3);
while (!p1.empty()) {
cout << p1.top().first << " "<<p1.top().second<<"\n";
p1.pop();
}
// 2 1
// 1 2
// 1 1
/* 小頂堆 vector<int> */
cout << "\n\n小頂堆 vector<int>:\n";
priority_queue<int,vector<int>,greater<int> >p2; //greater<>頭文件 #include<functional>
for (int i = 0; i < 10; i++)p2.push(a[i]);
while (!p2.empty()) {
cout << p2.top()<<" ";
p2.pop();
}
//0 1 2 3 4 5 6 7 8 9
/* 小頂堆 pair<int,int> */
cout << "\n\n小頂堆 pair<int,int>:\n"; //先比較first,相同的話,比較second
priority_queue<pair<int, int>,vector<pair<int,int> >,greater<pair<int,int> > >p3; //定義比較麻煩,別打錯了
pair<int, int>a4(1, 2);
pair<int, int>a5(1, 1);
pair<int, int>a6(2, 1);
p3.push(a4);
p3.push(a5);
p3.push(a6);
while (!p3.empty()) {
cout << p3.top().first << " " << p3.top().second << "\n";
p3.pop();
}
// 1 1
// 1 2
// 2 1
/* 小頂堆 結構體+重載< */
cout << "\n\n小頂堆 結構體+重載<:\n";
bool operator<(nod a, nod b);
priority_queue<nod>p4; //greater<>頭文件 #include<functional>
for (int i = 0; i < 10; i++)p4.push(nod(a[i],a[i]+3));
while (!p4.empty()) {
cout << p4.top().x << " "<<p4.top().y<<"\n";
p4.pop();
}
/*
0 3
1 4
2 5
3 6
4 7
5 8
6 9
7 10
8 11
9 12
*/
return 0;
}
bool operator<(nod a, nod b) {
return a.x == b.x ? a.y > b.y:a.x > b.x; //重載<運算符,先比較x,相等在比較y,如果a.y>b.y,返回true,那<成立,所以就是小頂堆
}
例題:合並果子
題目描述
在一個果園里,多多已經將所有的果子打了下來,而且按果子的不同種類分成了不同的堆。多多決定把所有的果子合成一堆。
每一次合並,多多可以把兩堆果子合並到一起,消耗的體力等於兩堆果子的重量之和。可以看出,所有的果子經過 n−1 次合並之后,就只剩下一堆了。
多多在合並果子時總共消耗的體力等於每次合並所耗體力之和。
因為還要花大力氣把這些果子搬回家,所以多多在合並果子時要盡可能地節省體力。
假定每個果子重量都為 1 ,並且已知果子的種類 數和每種果子的數目,你的任務是設計出合並的次序方案,使多多耗費的體力最少,並輸出這個最小的體力耗費值。
例如有 3 種果子,數目依次為 1 , 2 , 9 。可以先將 1 、 2 堆合並,新堆數目為 3 ,耗費體力為 3 。
接着,將新堆與原先的第三堆合並,又得到新的堆,數目為 12 ,耗費體力為 12 。所以多多總共耗費體力3+12=15 。可以證明 15 為最小的體力耗費值。
輸入輸出格式
輸入格式:共兩行。
第一行是一個整數 n(1≤n≤10000) ,表示果子的種類數。
第二行包含 n 個整數,用空格分隔,第 i 個整數 ai(1≤ai≤20000) 是第 i 種果子的數目。
輸出格式:一個整數,也就是最小的體力耗費值。輸入數據保證這個值小於 2^31 。
輸入輸出樣例
輸入樣例#1:
3 1 2 9
輸出樣例#1:
15
說明
對於30%的數據,保證有n≤1000:
對於50%的數據,保證有n≤5000;
對於全部的數據,保證有n≤10000。
#include<iostream>
#include<queue>
#include <functional> //greater<>頭文件
using namespace std;
int main() {
int n, a[10010],flag=0;
priority_queue<int, vector<int>, greater<int> > p; //建一個小頂堆
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
p.push(a[i]);
}
while (p.size() != 1) { //最后兩個相加,然后就把結果壓進小頂堆里了,最后里邊還有一個就是結果
int one = p.top();
p.pop();
int two = p.top();
p.pop();
p.push(one + two);
flag += one + two;
}
cout << flag<< "\n";
return 0;
}
