NYOJ 1128 手速(双端队列)

手速

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 1
描述

被学长虐了之后,wyl 认识到了手速的重要性,yy了一道。

初始化序列为空

给 n 个操作:

0 :    从头部往里放

1 :    从尾部往里放

2 :      从头部删除

3 :      从尾部删除

4:    改变功能,原来是从头部放的从尾部放,从尾部放的从头部放,删除也是如此

注:如果序列为空,请忽略2,3功能        

输入
Line1:T组数据(T<=10)
Line2:一个整数n(n<=200)
Line3:如果是0||1 操作,则后面跟一个m(int范围内),表示要放的数。
输出
如果当前序列为空,输出-1;
否则输出当前序列,用空格隔开
样例输入
2
3
0 1
0 2
1 3
0
样例输出
2 1 3
-1

deque<int> d:双端队列。

push_front();往头部插元素

push_back();往尾部插元素

pop_front();删除头部元素

pop_back();删除尾部元素


#include<cstdio>
#include<iostream>
#include<deque>
using namespace std;
int main() {
  int t; 
  cin >> t;
  while(t--) {
    int n, a, b, push_f = 0, push_b = 1, pop_f = 2, pop_b = 3;
    deque<int> d;
    cin >> n; 
    for(int i = 0; i < n; i++) {
      cin >> a;
      if(a == 4) {//改变功能 
        swap(push_f, push_b);
        swap(pop_f, pop_b);
      }
      else if(a == push_f) { cin >> b; d.push_front(b); } 
      else if(a == push_b) { cin >> b; d.push_back(b); }
      else if(a == pop_f && !d.empty()) d.pop_front();
      else if(a == pop_b && !d.empty()) d.pop_back();
    }
    if(d.empty()) cout << "-1" << endl;
    else {
      for(int i = 0; i < d.size(); i++)
        cout << d[i] << " ";
      cout << endl;
    }
  }
}



猜你喜欢

转载自blog.csdn.net/qq799028706/article/details/78634006
今日推荐