栈stack 和 队列queue

STL中,sort的默认排序为less,也就是说从小到大排序;priority_queue默认是less,也就说大顶堆;map默认是less,也就说用迭代器迭代的时候默认是小的排在前面;set默认是less,也就是说用迭代器迭代的时候是从小到大排序的。

 栈和队列的区别是啥? 吃多了拉就是队列,吃多了吐就是栈

栈(stack)又名堆栈,它是一种运算受限的线性表。其限
制是仅允许在表的一端进行插入和删除运算。这一端被称为
栈顶,相对地,把另一端称为栈底。向一个栈插入新元素又
称作进栈、入栈或压栈,它是把新元素放到栈顶元素的上
面,使之成为新的栈顶元素;从一个栈删除元素又称作出栈
或退栈,它是把栈顶元素删除掉,使其相邻的元素成为新的
栈顶元素。

sta.push();
que.push();
que.pop();
sta.pop();
sta.top();
que.front();
que.empty(); 若队列里空了返回1
sta.empty();  若栈里空了返回1

栈和队列的清空

while(!que.empty()) que.pop(); 
while(!sta.empty()) sta.pop();

我们知道了队列是先进先出,那么优先队列则不一样了,进
的顺序不能决定出的顺序,优先队列出的顺序是按照自己设
置的优先等级来出队列的,如果自己不设置优先级的话,默
认优先级为越大优先级越高。

优先队列代码

priority_queue <int> que;
que.push();
que.pop();
que.top(); 注意是top
que.empty();

优先级的设置

int 型

priority_queue <int,vector <int>,greater <int> >que;   最后> >之间要有空格 ,greater是小的做top
priority_queue <int,vector <int>,less<int> >que;  less是大的做top

//栈sta.top()
//队列que.front()
//优先队列q.top() 
#include<stdio.h>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;
stack<int> sta;
queue<int> que;
priority_queue<int,vector<int>,greater<int> > q;//最后两个> >之间要有空格 
int main()
{
	int n,x;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&x);
//		sta.push(x);
		q.push(x);
	}
//	printf("%d\n",sta.top());
//	printf("%d\n",que.front());
	while(!q.empty())
	{
		printf("%d\n",q.top());
		q.pop();//用了要清除 
	}
	return 0;
}

ACboy needs your help again!

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
#include<stdio.h>
#include<string.h>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,m;
		int i;
		int l=0;
		int a[500];
		char s[5];
		char s1[5];
		queue<int>que;
		stack<int>sta;
		int flag=0;
		scanf("%d%s",&n,s);
		for(i=1;i<=n;i++)
		{
			if(strcmp(s,"FIFO")==0)
			{
				flag=1;
				scanf("%s",s1);
				if(s1[0]=='O'&&que.empty())//s1为OUT 
					a[l++]=-444; //  队列为空的话赋值为负数,遇到这个数输出none。
				if(s1[0]=='I')
				{
					scanf("%d",&m);
					que.push(m);
				}
				else
				{
					if(!que.empty())
					{
						a[l++]=que.front();//遇到OUT用a数组存栈的首元素
						que.pop();//  弹栈
					}
				}
			}
			else
			{
				flag=2;
				scanf("%s",s1);
				if(s1[0]=='O'&&sta.empty())
					a[l++]=-444;
				if(s1[0]=='I')
				{
					scanf("%d",&m);
					sta.push(m);
				} 
				else
				{
					if(!sta.empty())
					{
						a[l++]=sta.top();
						sta.pop();
					}
				}
			}
		}	
		if(flag==1)
		{
			for(i=0;i<l;i++)
			{
				if(a[i]!=-444)
					printf("%d\n",a[i]);
				else
					printf("None\n");
			}
		}
		else if(flag==2)
		{
			for(i=0;i<l;i++)
			{
				if(a[i]!=-444)
					printf("%d\n",a[i]);
				else
					printf("None\n");
			}
		}
	} 
	return 0;
} 

The kth great number

Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round Xiao Ming can choose to write down a number, or ask Xiao Bao what the kth great number is. Because the number written by Xiao Ming is too much, Xiao Bao is feeling giddy. Now, try to help Xiao Bao.

Input

There are several test cases. For each test case, the first line of input contains two positive integer n, k. Then n lines follow. If Xiao Ming choose to write down a number, there will be an " I" followed by a number that Xiao Ming will write down. If Xiao Ming choose to ask Xiao Bao, there will be a "Q", then you need to output the kth great number. 

Output

The output consists of one integer representing the largest number of islands that all lie on one line. 

Sample Input

8 3
I 1
I 2
I 3
Q
I 5
Q
I 4
Q

Sample Output

1
2
3


        
  

Hint

Xiao  Ming  won't  ask  Xiao  Bao  the  kth  great  number  when  the  number  of  the  written number is smaller than k. (1=<k<=n<=1000000).

 用getchar();吸收回车  用cin>>a;代替scanf("%c",&a); 不知道为啥scanf老是出错

一轮循环结束后别忘记清空队列 

#include<stdio.h>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
priority_queue<int,vector<int>,greater<int> >q;
int main()
{
	int n,k;
	while(scanf("%d%d",&n,&k)!=EOF) 
	{
		char a;
		int j;
		getchar();//吸收回车 
		for(int i=0;i<n;i++)
		{
      		cin>>a;// 用scanf("%c",&a);不行 
			if(a=='I')
			{
				scanf("%d",&j);
				q.push(j);
			}
			if(q.size()>k)
				q.pop();
			if(a=='Q')
				printf("%d\n",q.top());
		}
		while(!q.empty()) //循环后别忘清空 
			q.pop();		
	}	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/VP_Death_Note/article/details/81231141