3187 队列练习 3

3187 队列练习 3

 

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 钻石 Diamond
 
 
 
题目描述 Description

比起第一题,本题加了另外一个操作,访问队头元素(编号3,保证访问队头元素时或出队时队不为空),现在给出这N此操作,输出结果。

输入描述 Input Description

N
N次操作(1入队 2出队 3访问队头)

输出描述 Output Description

K行(K为输入中询问的个数)每次的结果

样例输入 Sample Input

6
1 7
3
2
1 9
1 7
3

样例输出 Sample Output

7
9

数据范围及提示 Data Size & Hint

对于50%的数据 N≤1000 入队元素≤200
对于100%的数据 N≤100000入队元素均为正整数且小于等于10^4

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 int a[100001];
 5 int b[100001];
 6 int c[100001];
 7 int main()
 8 {
 9     int n;
10     scanf("%d",&n);
11     for(int i=1;i<=n;i++)
12      {
13          scanf("%d",&a[i]);
14          if(a[i]==1)
15           {
16               scanf("%d",&b[i]);
17           }
18      }
19      int heap=0,tail=0;
20      for(int i=1;i<=n;i++)
21       {
22           if(a[i]==1)
23            {
24                tail++;
25                c[tail]=b[i];
26            }
27            if(a[i]==2)
28             {
29                 heap++;
30             }
31             if(a[i]==3)
32              {
33                  cout<<c[heap+1]<<endl; 
34              }
35       }
36       return 0;
37 }
原文地址:https://www.cnblogs.com/lyqlyq/p/6665313.html