STL之三:队列和queue,优先队列和priority_queue

队列是基本的数据结构之一,特点是“先进先出”。

头文件:#include<queue>

队列的有关操作如下:

queue<Type> q;  //定义队列,Type为数据类型
q.push(item);  //把item放入队列
q.front();  //返回队首元素,但不会删除
q.pop();  //删除队首元素
q.back();  //返回队尾元素
q.size();  //返回队列元素的个数
q.empty();  //检查队列是否为空 

例题(hdu 1702):

Problem Description

ACboy was kidnapped!! 
he miss his mother very much and is very scare now.You can't image how dark the room he was put into is, so poor :(.
As a smart ACMer, you want to get ACboy out of the monster's labyrinth.But when you arrive at the gate of the maze, the monste say :" I have heard that you are very clever, but if can't solve my problems, you will die with ACboy."
The problems of the monster is shown on the wall:
Each problem's first line is a integer N(the number of commands), and a word "FIFO" or "FILO".(you are very happy because you know "FIFO" stands for "First In First Out", and "FILO" means "First In Last Out").
and the following N lines, each line is "IN M" or "OUT", (M represent a integer).
and the answer of a problem is a passowrd of a door, so if you want to rescue ACboy, answer the problem carefully!

Input

The input contains multiple test cases.
The first line has one integer,represent the number oftest cases.
And the input of each subproblem are described above.

Output

For each command "OUT", you should output a integer depend on the word is "FIFO" or "FILO", or a word "None" if you don't have any integer.

Sample Input

4 4 FIFO

IN 1

IN 2

OUT

OUT

4

FILO

IN 1

IN 2

OUT

OUT

5

FIFO

IN 1

IN 2

OUT

OUT

OUT

5

FILO

IN 1

IN 2

OUT

IN 3

OUT

Sample Output

1

2

2

1

1

2

None

2

3

题目大意:IN代表将后面的数压入队列或者堆栈,OUT便输出,是一道运用队列和栈的水题。

代码如下:

#include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin>>n;
	while(n--){
		int m;
		queue<int> q;  //定义队列q 
		stack<int> p;  //定义堆栈p 
		string s,s1="FIFO",s2="FILO";
		cin>>m>>s;  //输入字符串s与s1和s2比较,判断用队列还是堆栈 
		if(s==s1){  //队列,先进先出 
			while(m--){
				int k;
				string c,c1="IN",c2="OUT";  //判断是输入还是输出 
				cin>>c;
				if(c==c1){  //判断是输入 
					cin>>k;  //输入一个数 
					q.push(k);  //将这个数压入队列 
				}
				else if(c==c2){  //判断是输出 
					if(q.empty()) cout<<"None"<<endl;  //如果队列没有元素,输出None 
					else{  //输出队首元素 
						int t=q.front();  
						cout<<t<<endl;
						q.pop();
					}	
				}
			}
		}
		else if(s==s2){  //判断是运用堆栈,思路跟上一样,不再做注释了 
			while(m--){
				int k;
				string c,c1="IN",c2="OUT";
				cin>>c;
				if(c==c1){
					cin>>k;
					p.push(k);
				}
				else if(c==c2){
					if(p.empty()) cout<<"None"<<endl;
					else{
						int t=p.top();
						cout<<t<<endl;
						p.pop();
					}
				}
			}
		}
	} 
	return 0;
}

特别水的一道题,不过已经感觉STL大法相当方便了,STL大法好!

下面记录一下我学习的优先队列和priority_queue

优先队列:优先级最高的先出队(队列与排序的完美结合),它不仅可以存储数据,还可以把这些数据按照设定的规则进行排序,每次的push和pop操作,优先队列都会动态调整,把优先级最高的元素放在前面。

在STL中,优先队列是用二叉堆来实现的,在队列中push或pop一个数,复杂度都是O(\log_{2}n),但top操作快。

有关操作如下:

q.top();  //返回具有优先级最高的元素值,但不删除
q.pop();  //删除优先级最高的元素
q.push(item);  //插入新的元素 

PS:优先队列默认从大到小排列,如果要改变优先级顺序,可以进行如下操作:

queue<int> Q; //普通队列
queue<node> Q; //普通结构体队列
------------------------------------------------------- 
 
priority_queue<int> Q; //默认从大到小排列
priority_queue <int,vector<int>,less<int> > Q;//(注意两个">"之间要有空格)由大到小 
priority_queue <int,vector<int>,greater<int> > Q; //由小到大
--------------------------------------------------------

struct node
{ 
	int x,y;
	bool operator < (const node & a) const
	{ return x<a.x; } //若需要有小到大排序就把这一行的"<" 变为 ">"
 
};
priority_queue <node> Q;//结构体自定义自动排序队列 

例题(hdu 1873):

看病要排队这个是地球人都知道的常识。
不过经过细心的0068的观察,他发现了医院里排队还是有讲究的。0068所去的医院有三个医生(汗,这么少)同时看病。而看病的人病情有轻重,所以不能根据简单的先来先服务的原则。所以医院对每种病情规定了10种不同的优先级。级别为10的优先权最高,级别为1的优先权最低。医生在看病时,则会在他的队伍里面选择一个优先权最高的人进行诊治。如果遇到两个优先权一样的病人的话,则选择最早来排队的病人。

现在就请你帮助医院模拟这个看病过程。

Input

输入数据包含多组测试,请处理到文件结束。
每组数据第一行有一个正整数N(0<N<2000)表示发生事件的数目。
接下来有N行分别表示发生的事件。
一共有两种事件:
1:"IN A B",表示有一个拥有优先级B的病人要求医生A诊治。(0<A<=3,0<B<=10)
2:"OUT A",表示医生A进行了一次诊治,诊治完毕后,病人出院。(0<A<=3)

Output

对于每个"OUT A"事件,请在一行里面输出被诊治人的编号ID。如果该事件时无病人需要诊治,则输出"EMPTY"。
诊治人的编号ID的定义为:在一组测试中,"IN A B"事件发生第K次时,进来的病人ID即为K。从1开始编号。

Sample Input

7

IN 1 1

IN 1 2

OUT 1

OUT 2

IN 2 1

OUT 2

OUT 1

2

IN 1 1

OUT 1

Sample Output

2

EMPTY

3

1

1

代码如下:

#include<bits/stdc++.h>
using namespace std;
struct node{  //定义一个结构体 
	int x,y;  //x存放第几个来的,y存放他的优先级 
	bool operator <(const node& s)const{
		if(y!=s.y) return y<s.y;  //主排序:优先级大的在队首 
		return x>s.x;  //次排序:先来的在前面 
	}
};
int main(){
	int n;
	while(cin>>n){
		priority_queue<node>d[4];  //定义四个优先队列,模拟四个医生面前的排队 
		int t=1;
		node w;
		for(int i=0;i<n;i++){
			string s;
			cin>>s;
			if(s=="IN"){
				int a,b;  //输入要看的医生,优先级 
				cin>>a>>b;
				w.y=b;
				w.x=t;
				d[a].push(w);  //压入该医生的优先队列里进行排序 
				t++;
			}
			else if(s=="OUT"){
				int k;
				cin>>k;
				if(d[k].empty()) cout<<"EMPTY"<<endl;
				else{
					cout<<d[k].top().x<<endl;  //每次队首的都是优先级最大的,且最早来的 
					d[k].pop();
				}
			}
		}
	}
	return 0;
} 

这个题真的挺好的,感觉让我联系了刚学的优先队列,并且还扩展了结构体优先队列,要自己重载排序,还能进行次排序,觉得自己在一点点变强,哈哈哈。

STL大法好,跪舔~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值