//#define DEBUG #include using namespace std; struct linkedList{ struct node{ int data; node* next; node* prev; }; node* head; node* tail; int size; public: linkedList(){ head = NULL; tail = NULL; size = 0; } void insert(int data){ node* newNode = new node; newNode->data = data; newNode->next = NULL; newNode->prev = NULL; if(head == NULL){ head = newNode; tail = newNode; }else{ tail->next = newNode; newNode->prev = tail; tail = newNode; } size += 1; } void remove(node* nodeToRemove){ #ifdef DEBUG cout << "removing: " << nodeToRemove->data << endl; #endif if(nodeToRemove == NULL){ return; } if(nodeToRemove == head){ head = head->next; if(head != NULL){ head->prev = NULL; } }else if(nodeToRemove == tail){ tail = tail->prev; if(tail != NULL){ tail->next = NULL; } }else{ nodeToRemove->prev->next = nodeToRemove->next; nodeToRemove->next->prev = nodeToRemove->prev; } delete nodeToRemove; size -= 1; } void print(){ node* current = head; while(current != NULL){ cout << current->data << " "; current = current->next; } cout << endl; } }; linkedList::node* getAvailableMin(linkedList* p){ linkedList::node* current = p->head; linkedList::node* min = current; while(current != p->tail){ if(current->data < min->data){ min = current; } current = current->next; } return min; } int main(){ int N; cin >> N; linkedList p; vector q; for(int i = 0; i < N; i++){ int x; cin >> x; p.insert(x); } #ifdef DEBUG p.print(); #endif while(p.size > 0){ linkedList::node* min = getAvailableMin(&p); #ifdef DEBUG cout << "now list is: "; p.print(); cout << "min: " << min->data << endl; #endif q.push_back(min->data); q.push_back(min->next->data); p.remove(min->next); p.remove(min); } for(int i = 0; i < q.size(); i++){ cout << q[i] << " "; } return 0; }