queue-概述:
队列中没有元素时,称为空队列。
bool empty() |
队列为空返回true,否则返回false |
void pop() |
删除队列的一个元素 |
void push( const TYPE &val ) |
将val元素加入队列 |
size_type size() |
返当前队列中的元素数目 |
TYPE &back() |
返回一个引用,指向队列的最后一个元素 |
TYPE &front() |
返回队列第一个元素的引用 |
bool empty() |
优先队列为空返回true,否则返回false |
void pop() |
删除优先队列中的第一个元素 |
void push( const TYPE &val ) |
添加一个元素到优先队列中,值为val |
size_type size() |
返当前队列中的元素数目 |
TYPE &top () |
返回一个引用,指向最高优先级的元素 |
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<queue> 3 #include<set> 4 #include<vector> 5 using namespace std; 6 7 void parse(vector<set<int> >&adj, unsigned int p=0)//十分巧妙地递归读入。 8 { 9 unsigned int x; 10 cin>>x; 11 if(p) 12 { 13 adj[p].insert(x); 14 adj[x].insert(p); 15 } 16 while(true) 17 { 18 char ch; 19 cin>>ch; 20 if(ch==')') break; 21 parse(adj, x); 22 } 23 return; 24 } 25 26 int main() 27 { 28 //freopen( "in.txt", "r", stdin ); 29 //freopen( "out.txt", "w", stdout ); 30 char ch; 31 while(cin>>ch) 32 { 33 vector<set<int> > adj(1024, set<int>()); 34 parse(adj); 35 priority_queue<int, vector<int>, greater<int> >leafs; 36 int n = 0; 37 for(unsigned int i = 0; i<adj.size();i++) 38 if(adj[i].size()) 39 { 40 n++; 41 if(adj[i].size()==1) 42 leafs.push(i); 43 } 44 for(int k=1; k<n; k++) 45 { 46 unsigned int x = leafs.top(); 47 leafs.pop(); 48 unsigned int p = *(adj[x].begin()); 49 if(k>1) 50 cout<<" "; 51 cout<<p; 52 adj[p].erase(x); 53 if(adj[p].size()==1) 54 leafs.push(p); 55 } 56 cout<<endl; 57 } 58 return 0; 59 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<algorithm> #include<vector> #include<queue> #include<cstdio> #include<iostream> using namespace std; int main() { int n, k; while(~scanf("%d%d", &n, &k)) { priority_queue<int, vector<int>, greater<int> >que; while(n--) { char op[5]; scanf("%s", op); if(op[0]=='I') { int val; scanf("%d", &val); que.push(val); while(que.size()>k) que.pop(); } else printf("%d ", que.top()); } } return 0; }
(提醒:不要直接交代码, 上面代码不能通过杭电的编译器)。
然后稍加改变, 编写自己的比较函数,而不是用自带的算子(greater)。 结果就过啦! 也是十分的蛋疼,十分的无语!!!。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<algorithm> #include<vector> #include<queue> #include<cstdio> #include<iostream> using namespace std; struct Cmp{ bool operator()(const int&t1, const int&t2) { return t1>t2; } }; int main() { int n, k; while(~scanf("%d%d", &n, &k)) { priority_queue<int, vector<int>,Cmp>que; while(n--) { char op[5]; scanf("%s", op); if(op[0]=='I') { int val; scanf("%d", &val); que.push(val); while(que.size()>k) que.pop(); } else printf("%d ", que.top()); } } return 0; }
上面这个代码也不太好, 因为它有了太多的进队和出队, 每一次的进出都是需要维护优先队列的, 所以可以在入队满K个后, 在后来的插入时, 可以先比较一下, 然后决定是否插入。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<algorithm> #include<vector> #include<queue> #include<cstdio> #include<iostream> using namespace std; struct Cmp{ bool operator()(const int&t1, const int&t2) { return t1>t2; } }; int main() { int n, k; while(~scanf("%d%d", &n, &k)) { priority_queue<int, vector<int>,Cmp>que; int t = k; char op[5]; int val; while(t--) { scanf("%s", &op); scanf("%d", &val); que.push(val); } n = n-k; while(n--) { scanf("%s", &op); if(op[0]=='I') { scanf("%d", &val); if(val>que.top()) que.push(val), que.pop(); } else printf("%d ", que.top()); } } return 0; }
一般优先队列的定义方法:
priority_queue< Type,vector<Type>,greater<Type> >(小顶堆)
priority_queue< Type,vector<Type>,less<Type> >(大顶堆)
如果是自定义的结构体类型
priority_queue<Node,vector<Node>,cmp>
需要自己自定义结构体类型:
struct cmp
{
bool operator()(const Node &t1,const Node &t2)
{
return t1.b<t2.b;//相当于less,大顶堆
}
};
3.又是一道可以用优先队列解的题。 优先队列的功能还真强大。
http://acm.hdu.edu.cn/showproblem.php?pid=4393
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio> #include<iostream> #include<queue> #include<algorithm> using namespace std; struct Node{ int F; int index; friend bool operator<(Node a, Node b) { if(a.F!=b.F) return a.F<b.F; else return a.index>b.index; } }; priority_queue<Node>q[110]; int main() { int T; int kase, n, S; Node a; scanf("%d", &T); for(kase=1; kase<=T; kase++) { scanf("%d", &n); for(int i=1; i<=n; i++) { scanf("%d%d", &a.F, &S); a.index = i; q[S].push(a); } printf("Case #%d: ", kase); for(int i=0; i<n; i++) { int fast = -1, id = 1; for(int j=1; j<=100; j++) if(!q[j].empty()) { Node tmp=q[j].top(); if(tmp.F+i*j>fast) fast=tmp.F+i*j, id=j; else if(tmp.F+i*j==fast&&tmp.index<q[id].top().index) id = j; } printf("%d", q[id].top().index); q[id].pop(); if(i<n-1)printf(" "); else printf(" "); } } return 0; }