#include #include #include using std::cin; using std::cout; const int ChunkSize = 4; // Узел односвязного списка. struct CNode { int Data[ChunkSize]; CNode* Next; explicit CNode() : Next( 0 ) { ::memset( Data, sizeof( Data ), 0 ); } }; // Очередь на основе односвязного списка. class CQueue { public: CQueue(); ~CQueue(); // Добавление в конец очереди. void Enqueue( int a ); // Извлечение. Если очередь пуста, возвращает -1. int Dequeue(); private: CNode* head; // Указатель на голову очереди. 0, если очередь пуста. CNode* tail; // Указатель на хвост очереди. 0, если очередь пуста. int firstElementIndex; int lastChunkElementsCount; }; CQueue::CQueue() : head( new CNode ), tail( head ), firstElementIndex( 0 ), lastChunkElementsCount( 0 ) { } CQueue::~CQueue() { for( CNode* current = head; current != 0; ) { head = current->Next; delete current; current = head; } } void CQueue::Enqueue( int a ) { if( lastChunkElementsCount == ChunkSize ) { tail->Next = new CNode; tail = tail->Next; lastChunkElementsCount = 0; } // Место в последнем чанке есть. assert( lastChunkElementsCount < ChunkSize ); tail->Data[lastChunkElementsCount++] = a; } int CQueue::Dequeue() { if( head == tail && firstElementIndex == lastChunkElementsCount ) { return -1; } assert( firstElementIndex < ChunkSize ); int toReturn = head->Data[firstElementIndex]; firstElementIndex++; if( firstElementIndex == ChunkSize ) { firstElementIndex = 0; if( head == tail ) { lastChunkElementsCount = 0; } else { CNode* toDelete = head; head = head->Next; delete toDelete; } } return toReturn; } int main() { std::iostream::sync_with_stdio( false ); int commandsCount = 0; cin >> commandsCount; CQueue queue; for( int i = 0; i < commandsCount; ++i ) { int command = 0; int value = 0; cin >> command >> value; switch( command ) { case 2: if( queue.Dequeue() != value ) { cout << "NO"; return 0; } break; case 3: queue.Enqueue( value ); break; } } cout << "YES"; return 0; }