双端队列 【deque】

题目链接:https://ac.nowcoder.com/acm/contest/1071/D

还是第一次简单运用双端队列。顾名思义:队列的头尾都可以进行插入跟删除操作。

存在于头文件 deque 中。

 1 #include<iostream>
 2 #include<deque>
 3 #include<string.h>
 4 using namespace std;
 5 
 6 char a, b;
 7 int x;
 8 deque<int> Q;
 9 
10 int main()
11 {
12     cin.sync_with_stdio(false);
13     int n, id = 0;
14     cin >> n;
15     while(n --)
16     {
17         cin >> a;
18         if(a == 'A')
19         {
20             cin >> b;
21             if(b == 'L')
22                 Q.push_front(++ id);
23             else
24                 Q.push_back(++ id);
25         }
26         else
27         {
28             cin >> b >> x;
29             if(b == 'L')
30                 while(x --)
31                     Q.pop_front();
32             else
33                 while(x --)
34                     Q.pop_back();
35         }
36     }
37     while(!Q.empty())
38     {
39         x = Q.front();
40         cout << x << endl;
41         Q.pop_front();
42     }
43     return 0;
44 }
deque的简单操作

 

posted @ 2019-08-12 12:22  缘未到  阅读(204)  评论(0编辑  收藏  举报