acm的STL容器之队列篇 及 UVa 11292

2018-07-17

优先队列,即Priority Queues

1.简单介绍一下队列(介绍功能,不作分析)

C++队列是一种容器适配器,它给予程序员一种先进先出(FIFO)的数据结构。
1.back() 返回一个引用,指向最后一个元素
2.empty() 如果队列空则返回真
3.front() 返回第一个元素
4.pop() 删除第一个元素
5.push() 在末尾加入一个元素
6.size() 返回队列中元素的个数

简介:队列可以用线性表(list)或双向队列(deque)来实现(注意vector container 不能用来实现queue,因为vector 没有成员函数pop_front!):
queue<list<int>> q1;
queue<deque<int>> q2;
其成员函数有“判空(empty)” 、“尺寸(Size)” 、“首元(front)” 、“尾元(backt)” 、“加入队列(push)” 、“弹出队列(pop)”等操作。

 int main()
{
     queue<int> q;
     q.push(4);
     q.push(5);
     printf("%d
",q.front());
     q.pop();
 }

2.双向队列deque

deque双向队列是一种双向开口的连续线性空间,可以高效的在头尾两端插入和删除元素,deque在接口上和vector非常相似,使用比较少,暂无心得,只列简单用法,不作讨论。

 1 //双向队列 deque
 2 //by MoreWindows http://blog.csdn.net/morewindows
 3 #include <deque>
 4 #include <cstdio>
 5 #include <algorithm>
 6 using namespace std;
 7 int main()
 8 {
 9     deque<int> ideq(20); //Create a deque ideq with 20 elements of default value 0
10     deque<int>::iterator pos;
11     int i;
12  
13     //使用assign()赋值  assign在计算机中就是赋值的意思
14     for (i = 0; i < 20; ++i)
15         ideq[i] = i;
16     
17     //输出deque
18     printf("输出deque中数据:
");
19     for (i = 0; i < 20; ++i)
20         printf("%d ", ideq[i]);
21     putchar('
');
22  
23     //在头尾加入新数据
24     printf("
在头尾加入新数据...
");
25     ideq.push_back(100);
26     ideq.push_front(i);
27  
28     //输出deque
29     printf("
输出deque中数据:
");
30     for (pos = ideq.begin(); pos != ideq.end(); pos++)
31         printf("%d ", *pos);
32     putchar('
');
33  
34     //查找
35     const int FINDNUMBER = 19;
36     printf("
查找%d
", FINDNUMBER);
37     pos = find(ideq.begin(), ideq.end(), FINDNUMBER);
38     if (pos != ideq.end())
39         printf("find %d success
", *pos);
40     else
41         printf("find failed
");
42  
43     //在头尾删除数据
44     printf("
在头尾删除数据...
");
45     ideq.pop_back();
46     ideq.pop_front();
47  
48     //输出deque
49     printf("
输出deque中数据:
");
50     for (pos = ideq.begin(); pos != ideq.end(); pos++)
51         printf("%d ", *pos);
52     putchar('
');
53     return 0;
54 }

3.Priority Queues(优先队列)

C++优先队列类似队列,但是在这个数据结构中的元素按照一定的断言排列有序。
1.empty() 如果优先队列为空,则返回真
2.pop() 删除第一个元素
3.push() 加入一个元素
4.size() 返回优先队列中拥有的元素的个数
5.top() 返回优先队列中有最高优先级的元素

优先级队列可以用向量(vector)或双向队列(deque)来实现(注意list container 不能用来实现queue,因为list 的迭代器不是任意存取iterator,而pop 中用到堆排序时是要求randomaccess iterator 的!):
priority_queue<vector<int>, less<int>> pq1; // 使用递增less<int>函数对象排序
priority_queue<deque<int>, greater<int>> pq2; // 使用递减greater<int>函数对象排序
其成员函数有“判空(empty)” 、“尺寸(Size)” 、“栈顶元素(top)” 、“压栈(push)” 、“弹栈(pop)”等。(建议包含头文件<functional>)

1 #include <iostream>
 2 #include <queue> 
 3 using namespace std;
 4  
 5 class T {
 6 public:
 7     int x, y, z; 
 8     T(int a, int b, int c):x(a), y(b), z(c)
 9     { 
10     }
11 };
12 bool operator < (const T &t1, const T &t2) 
13 {
14     return t1.z < t2.z; // 按照z的顺序来决定t1和t2的顺序
15 } 
16 main()
17 { 
18     priority_queue<T> q; 
19     q.push(T(4,4,3)); 
20     q.push(T(2,2,5)); 
21     q.push(T(1,5,4)); 
22     q.push(T(3,3,6)); 
23     while (!q.empty()) 
24     { 
25         T t = q.top(); 
26         q.pop(); 
27         cout << t.x << " " << t.y << " " << t.z << endl; 
28     } 
29     return 1; 
30 }

4.利用优先队列pq实现大根堆和小根堆

虽然在很多编译器中,只需要像“3”一样include头文件就可以了,可是在oj中,我们使用优先队列必须要包含<functional>头文件。

 1 #include<iostream>
 2 #include<functional>
 3 #include<queue>
 4 using namespace std;
 5 //实现小根堆
 6 int main(){
 7     priority_queue<int,vector<int> , greater<int> > ipq;//注意great<int>和后面的>中间要有空格,否则会报错。
 8     ipq.push(3); 
 9     ipq.push(4);
10     ipq.push(3);
11     ipq.push(6);
12     ipq.push(7);
13     ipq.push(5);
14     while(!ipq.empty()){
15         cout<<ipq.top()<<endl;
16         ipq.pop();
17     }
18 }

实现大根堆代码和上述差不多,但是要把第7行声明改一下

 1 priority_queue<int,vector<int> , less<int> > ipq; 

 那么优先队列所实现的大根堆和小根堆有什么用呢?

如果题目要求如下:不停地给你int型整数 即

 1 int a;

2 while(cin>>a) 

如果a是0,输出目前数组的最大值和最小值,如果a是-1,结束。

这个时候直接用大(小)根堆就可以搞定。

 

你或许会说,这题就是为了使用优先队列而创建的

那好,我们来看一下实例

在UVa 11292 - Dragon of Loowater 问题中,我们可以使用小根堆

题意:
有n个头的恶龙,你雇佣一些骑士把所有的头砍掉,一共有m个骑士可以雇佣,
一个能力为x的骑士只能砍下恶龙一个直径不超过x的头,而且要支付x个金币。
问:如何雇佣骑士才能砍掉所有恶龙的头,而且要使付出的金币最少。

注意:一个骑士只能砍掉一个头,而且只能被雇用一次!

输入为:
n m
接下来n行为恶龙的头的直径。
再接下来是m行骑士的能力。

如果不能看下所有恶龙的头,输出:
“Loowater is doomed!”
否则输出最少花费

思路:利用2个小根堆,分别存龙和骑士的数值。然后弹出龙和骑士的数值x和y,如果龙x小于等于骑士y,则维护一个sum值。如果龙x大于骑士y,则弹出下一个骑士的数值。

如果龙x的小根堆空了,就输出sum,如果骑士的小根堆先空了,输出“Loowater is doomed!”

附上小根堆ac代码

 1 #include<iostream>
 2 #include<functional>
 3 #include<queue>
 4 using namespace std;
 5 
 6 int main(){
 7     
 8     int n,m,tmp,x,y;
 9     while(cin>>n>>m){
10         if(n==0&&m==0) break;
11         priority_queue<int,vector<int> , greater<int> > dragon;
12         priority_queue<int,vector<int> , greater<int> > knight;
13         int cost=0;
14         int succeed=0;
15         for(int i=0;i<n;i++){
16             cin>>tmp;
17             dragon.push(tmp);
18         }
19         for(int i=0;i<m;i++){
20             cin>>tmp;
21             knight.push(tmp);
22         }
23         while(!knight.empty()){//当还有能力值更高的骑士的时候
24             x=dragon.top();//当前最弱小的龙
25             y=knight.top();knight.pop();//派当前最弱的骑士,无论屠龙与否,下一次都会派更强的骑士
26             if(x<=y){//如果屠龙成功
27                 cost+=y;//支付报酬
28                 dragon.pop();//这条龙没了,出队列
29             }
30             if(dragon.empty()){//龙屠干净了
31                 cout<<cost<<endl;//输出总费用
32                 succeed=1;//成功了
33                 break;
34             }
35         }
36         if(!succeed) cout<<"Loowater is doomed!"<<endl;  //如果没成功,输出失败  
37     }
38 return 0;
39 }
原文地址:https://www.cnblogs.com/VsKendo/p/9320512.html