Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Реализовать очередь с помощью двух стеков. Использовать стек, реализованный с помощью динамического буфера.
- */
- #include <iostream>
- #include <assert.h>
- class Stack {
- public:
- Stack(int _count);// Конструктор
- ~Stack();// Деструктор
- Stack(const Stack& other);// Конструктор копирования
- Stack(Stack&& other);// Конструктор перемещения, noexcept - для оптимизации при использовании стандартных контейнеров
- Stack& operator=(const Stack& other);// Оператор присваивания копированием
- Stack& operator=(Stack&& other);// Оператор присваивания перемещением
- void Push(int value);
- int Top() const { return top; };
- int Pop();
- bool IsEmpty() const { return top == -1; };
- private:
- int* buffer;
- int count; //размер буфера
- int top; //индекс верхнего элемента
- void grow();
- void clear();
- };
- void Stack::clear() {
- delete[] buffer;
- top = -1;
- count = 0;
- }
- Stack::Stack(int _count = 1) : count(_count), top (-1) {
- buffer = new int[count];
- }
- Stack::~Stack()
- {
- delete[] buffer;
- }
- Stack::Stack(const Stack& other){
- buffer = new int[count];
- int k = 0;
- while (k < count) {
- buffer[k] = other.buffer[k];
- ++k;
- }
- top = other.top;
- count = other.count;
- }
- Stack::Stack(Stack&& other) {
- buffer = other.buffer;
- other.buffer = nullptr;
- top = other.top;
- other.top = 0;
- count = other.count;
- other.count = 0;
- }
- Stack& Stack::operator=(const Stack& other){
- int* new_buffer = new int[count];
- int k = 0;
- while (k < count) {
- new_buffer[k] = other.buffer[k];
- ++k;
- }
- delete[] buffer;
- top = other.top;
- buffer = new_buffer;
- return *this;
- }
- Stack& Stack::operator=(Stack&& other){
- clear();
- buffer = other.buffer;
- other.buffer = nullptr;
- top = other.top;
- other.top = 0;
- count = other.count;
- other.count = 0;
- return *this;
- }
- void Stack::grow() {
- int* newbuffer = new int[count * 2];
- for (int i = 0; i < top + 1; i++) {
- newbuffer[i] = buffer[i];
- }
- delete[] buffer;
- buffer = newbuffer;
- count *= 2;
- }
- void Stack::Push(int value)
- {
- if (top >= count - 1) {
- grow();
- }
- top++;
- buffer[top] = value;
- }
- int Stack::Pop()
- {
- assert(!IsEmpty());
- int a = buffer[top];
- top--;
- return a;
- }
- class Queue {
- public:
- Queue(int count) : left(count), right(count) {};
- void Push(int value);
- int Pop();
- bool IsEmpty() const;
- private:
- int count;
- Stack left;
- Stack right;
- };
- Queue::Queue(int count) : count(count) { }
- bool Queue::IsEmpty() const {
- return (left.IsEmpty() && right.IsEmpty());
- }
- void Queue::Push(int value) {
- left.Push(value);
- }
- int Queue::Pop() {
- if (IsEmpty()) return -1;
- if (right.IsEmpty()) {
- while(!left.IsEmpty()) {
- right.Push(left.Pop());
- }
- }
- return right.Pop();
- }
- bool solv(int n, int* a, int* b, Queue &q) {
- for (int i = 0; i < n; i++) {
- int result = 0;
- if (a[i] == 2) {
- result = q.Pop();
- if (result != b[i]) return false;
- }
- if (a[i] == 3) q.Push(b[i]);
- }
- return true;
- }
- int main() {
- int n = 0;
- std::cin >> n;
- int* a = new int [n];
- int* b = new int [n];
- Queue q(n);
- for (int i = 0; i < n; i++) {
- std::cin >> a[i] >> b[i];
- }
- bool result = solv(n, a, b, q);
- if (result) std::cout << "YES";
- else std::cout << "NO";
- delete[] a;
- delete[] b;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement