公式解説
By
tomo8
これは両端キューを題材にした問題です。
C++ には std::deque があります。
各クエリは、
- 1:
push_back(x) - 2:
push_front(x) - 3:
front(), pop_front()
を使うことで、極めて容易に実装できます。
解答 (C++)
#include <bits/stdc++.h> using namespace std; int main() { int q; cin >> q; deque<int> d; while (q--) { int t; cin >> t; if (t == 3) { cout << d.front() << endl; d.pop_front(); } else { int x; cin >> x; if (t == 1) d.push_back(x); else d.push_front(x); } } }