C++优先队列(大根堆小根堆)
常见四种形式:
priority_queue<int> p1; priority_queue<PII> p2; priority_queue<PII,vector<PII>,greater<PII> > p3; priority_queue<PII,vector<PII>,less<PII> > p4;
priority_queue<int> p1;
大根堆:从大到小排序
priority_queue<int> p1;
p1.push(1);
p1.push(2);
p1.push(5);
p1.push(3);
while(!p1.empty()){
int t = p1.top();
p1.pop();
cout << t << endl;
}
cout << "=================" << endl;
priority_queue<PII> p2;
大根堆:按照first从大到小排序
priority_queue<PII> p2;
p2.push(mp(1,2));
p2.push(mp(2,4));
p2.push(mp(3,1));
p2.push(mp(4,2));
p2.push(mp(5,4));
p2.push(mp(6,1));
while(!p2.empty()){
PII t = p2.top();
p2.pop();
cout << t.fi << " " << t.se << endl;
}
cout << "=================" << endl;
priority_queue<PII,vector<PII>,greater<PII> > p3;
小根堆:按照first从小到大排序
priority_queue<PII,vector<PII>,greater<PII> > p3;
p3.push(mp(1,2));
p3.push(mp(2,4));
p3.push(mp(3,1));
p3.push(mp(4,2));
p3.push(mp(5,4));
p3.push(mp(6,1));
while(!p3.empty()){
PII t = p3.top();
p3.pop();
cout << t.fi << " " << t.se << endl;
}
cout << "=================" << endl;
priority_queue<PII,vector<PII>,less<PII> > p4;
大根堆:跟默认一样
priority_queue<PII,vector<PII>,less<PII> > p4;
p4.push(mp(1,2));
p4.push(mp(2,4));
p4.push(mp(3,1));
p4.push(mp(4,2));
p4.push(mp(5,4));
p4.push(mp(6,1));
while(!p4.empty()){
PII t = p4.top();
p4.pop();
cout << t.fi << " " << t.se << endl;
}
cout << "=================" << endl;
|