#include <iostream> #include <fstream> #include <string> #include <sstream> using namespace std; struct Point { int x,y; friend std::ostream &operator << (std::ostream &output,const Point &p); friend std::istream &operator >> (std::istream &input,Point &p); }; bool operator == (Point a,Point b) { return (a.x == b.x && a.y == b.y); } std::ostream &operator << (std::ostream &output,const Point &p) { output<<"( "<<p.x<<" , "<<p.y<<" ) "; return output; } std::istream &operator >> (std::istream &input,Point &p) { input>>p.x>>p.y;; return input; } template<class T> class Link { private: T data; Link<T> *next; Link<T> *previous; public: Link(T data); void displayLink(); T getData(); void setData(T data); Link<T> *getNext(); void setNext(Link<T> *next); Link<T> *getPrevious(); void setPrevious(Link<T> *previous); template<class U> friend std::ostream& operator<< (std::ostream& print, Link<U>& obj); }; template<class T> Link<T>::Link(T data) { setData(data); setNext(NULL); setPrevious(NULL); } template<class T> void Link<T>::displayLink() { std::cout<<getData()<<" "; } template<class T> T Link<T>::getData() { return data; } template<class T> void Link<T>::setData(T data) { this->data = data; } template<class T> Link<T> *Link<T>::getNext() { return next; } template<class T> void Link<T>::setNext(Link<T> *next) { this->next = next; } template<class T> Link<T> *Link<T>::getPrevious() { return previous; } template<class T> void Link<T>::setPrevious(Link<T> *previous) { this->previous = previous; } template <class U> std::ostream& operator<<(std::ostream& print, Link<U> & L) { print << L.getData(); return print; } template<class T> class DoublyLinkedList { private: Link<T> *first; Link<T> *last; public: DoublyLinkedList(); ~DoublyLinkedList(); Link<T> *getFirst(); void setFirst(Link<T> *first); Link<T> *getLast(); void setLast(Link<T> *first); bool isEmpty(); Link<T> *find(T key); void insertFirst(T dd); void insertLast(T dd); void deleteFirst(); void deleteLast(); bool insertAfter(T key,T dd); void deleteKey(T key); void displayForward(); void displayBackward(); }; template<class T> DoublyLinkedList<T>::DoublyLinkedList() { setFirst(NULL); setLast(NULL); } template<class T> DoublyLinkedList<T>::~DoublyLinkedList() { while(!isEmpty()) { deleteFirst(); } } template<class T> Link<T> *DoublyLinkedList<T>::getFirst() { return first; } template<class T> void DoublyLinkedList<T>::setFirst(Link<T> *first) { this->first = first; } template<class T> Link<T> *DoublyLinkedList<T>::getLast() { return last; } template<class T> void DoublyLinkedList<T>::setLast(Link<T> *last) { this->last = last; } template<class T> bool DoublyLinkedList<T>::isEmpty() { return getFirst()==NULL; } template<class T> Link<T> *DoublyLinkedList<T>::find(T key) { Link<T> *current = getFirst(); while(current != NULL && !(current->getData == key)) current = current->getNext(); return current; } template<class T> void DoublyLinkedList<T>::insertFirst(T dd) { Link<T> *newLink = new Link<T>(dd); if(isEmpty()) setLast(newLink); else getFirst()->setPrevious(newLink); newLink->setNext(getFirst()); setFirst(newLink); } template<class T> void DoublyLinkedList<T>::insertLast(T dd) { Link<T> *newLink = new Link<T>(dd); if(isEmpty()) { setFirst(newLink); } else { getLast()->setNext(newLink); newLink->setPrevious(getLast()); } setLast(newLink); } template<class T> void DoublyLinkedList<T>::deleteFirst() { if(isEmpty()) return; Link<T> *temp = getFirst(); if(getFirst()->getNext() == NULL) setLast(NULL); else getFirst()->getNext()->setPrevious(NULL); setFirst(getFirst()->getNext()); delete temp; } template<class T> void DoublyLinkedList<T>::deleteLast() { if(isEmpty()) return; Link<T> *temp = getLast(); if(getFirst()->getNext() == NULL) setFirst(NULL); else getLast()->getPrevious()->setNext(NULL); setLast(getLast()->getPrevious()); delete temp; } template<class T> bool DoublyLinkedList<T>::insertAfter(T key,T dd) { Link<T> *current = find(key); if(current == NULL) return false; Link<T> *newLink = new Link<T>(dd); if(current->getNext() == NULL) { newLink->setNext(NULL); setLast(newLink); } else { newLink->setNext(current->getNext()); current->getNext()->setPrevious(newLink); } newLink->setPrevious(current); current->setNext(newLink); return true; } template<class T> void DoublyLinkedList<T>::deleteKey(T key) { Link<T> *current = find(key); if(current==NULL) return; if(current->getPrevious() == NULL) setFirst(current->getNext()); else current->getPrevious()->setNext(current->getNext()); if(current->getNext() == NULL) setLast(current->getPrevious()); else current->getNext()->setPrevious(current->getPrevious()); delete current; } template<class T> void DoublyLinkedList<T>::displayForward() { std::cout<<"List (first-->last): "; Link<T> *current = getFirst(); while(current != NULL) { current->displayLink(); current = current->getNext(); } std::cout<<"\n"; } template<class T> void DoublyLinkedList<T>::displayBackward() { std::cout<<"List (last-->first): "; Link<T> *current = getLast(); while(current != NULL) { current->displayLink(); current = current->getPrevious(); } std::cout<<"\n"; } int vect(Point a1,Point a2,Point b1,Point b2) { return (a2.x - a1.x)*(b2.y - b1.y) - (b2.x - b1.x)*(a2.y - a1.y); } int dist2(Point a1,Point a2) { return (a2.x - a1.x)*(a2.x - a1.x) + (a2.y - a1.y)*(a2.y - a1.y); } void Solve(DoublyLinkedList<Point> &A,DoublyLinkedList<Point> &B) { Link<Point> *p,*q,*m,*min; if(!A.isEmpty()) { m = A.getFirst(); p = m->getNext(); while(p != NULL) { if(p->getData().y < m->getData().y) m = p; else if(p->getData().y == m->getData().y && p->getData().x > m->getData().x) m = p; p = p->getNext(); } if(m->getPrevious() == NULL) A.setFirst(m->getNext()); else m->getPrevious()->setNext(m->getNext()); if(m->getNext() == NULL) A.setLast(m->getPrevious()); else m->getNext()->setPrevious(m->getPrevious()); if(A.isEmpty()) A.setLast(m); else A.getFirst()->setPrevious(m); m->setNext(A.getFirst()); m->setPrevious(NULL); A.setFirst(m); B.insertLast(A.getFirst()->getData()); if(A.getFirst()->getNext() != NULL) min = A.getFirst()->getNext(); else min = A.getFirst(); do { q = A.getFirst()->getNext(); while(q != NULL) { if((vect(B.getLast()->getData(),min->getData(),B.getLast()->getData(),q->getData()) < 0)|| (vect(B.getLast()->getData(),min->getData(),B.getLast()->getData(),q->getData()) == 0)&& (dist2(B.getLast()->getData(),min->getData()) < dist2(B.getLast()->getData(),q->getData()))) min = q; q = q->getNext(); } B.insertLast(min->getData()); min = A.getFirst(); } while(!(B.getLast()->getData() == B.getFirst()->getData())); B.deleteLast(); } } int main(int argc, char *argv[]) { DoublyLinkedList<Point> A; DoublyLinkedList<Point> B; stringstream ss; Point p; string line; int counter; while(getline(cin,line)) { ss.clear(); ss.str(line); counter = 1; while(ss) { try { switch(counter) { case 1: { ss>>p.x; break; } case 0: { ss>>p.y; A.insertLast(p); break; } } } catch(...) { } counter = (counter + 1)%2; } Solve(A,B); cout<<"List A \n"; A.displayForward(); A.displayBackward(); cout<<"List B \n"; B.displayForward(); B.displayBackward(); while(!A.isEmpty()) A.deleteFirst(); while(!B.isEmpty()) B.deleteFirst(); } return EXIT_SUCCESS; }
573 40 686 88 733 276 641 169 695 368 656 274 625 355 592 274 563 259 540 409 494 491 517 359 532 173 405 403 379 455 464 217 538 95 256 488 349 351 406 259 259 390 333 307 183 434 331 243 164 374 235 316 54 460 279 247 313 204 65 342 118 306 220 242 165 244 330 146 17 264 91 234 225 149 153 171 447 73 76 159 280 94 174 71 1 1 4 3 2 0 2 2 4 1 3 2 3 4 1 3 2 3 187 141 247 331 91 315 324 187 80 166 337 308 210 437 284 270 370 377 413 217 195 332 418 458 292 324 217 186
List A List (first-->last): ( 573 , 40 ) ( 686 , 88 ) ( 733 , 276 ) ( 641 , 169 ) ( 695 , 368 ) ( 656 , 274 ) ( 625 , 355 ) ( 592 , 274 ) ( 563 , 259 ) ( 540 , 409 ) ( 494 , 491 ) ( 517 , 359 ) ( 532 , 173 ) ( 405 , 403 ) ( 379 , 455 ) ( 464 , 217 ) ( 538 , 95 ) ( 256 , 488 ) ( 349 , 351 ) ( 406 , 259 ) ( 259 , 390 ) ( 333 , 307 ) ( 183 , 434 ) ( 331 , 243 ) ( 164 , 374 ) ( 235 , 316 ) ( 54 , 460 ) ( 279 , 247 ) ( 313 , 204 ) ( 65 , 342 ) ( 118 , 306 ) ( 220 , 242 ) ( 165 , 244 ) ( 330 , 146 ) ( 17 , 264 ) ( 91 , 234 ) ( 225 , 149 ) ( 153 , 171 ) ( 447 , 73 ) ( 76 , 159 ) ( 280 , 94 ) ( 174 , 71 ) List (last-->first): ( 174 , 71 ) ( 280 , 94 ) ( 76 , 159 ) ( 447 , 73 ) ( 153 , 171 ) ( 225 , 149 ) ( 91 , 234 ) ( 17 , 264 ) ( 330 , 146 ) ( 165 , 244 ) ( 220 , 242 ) ( 118 , 306 ) ( 65 , 342 ) ( 313 , 204 ) ( 279 , 247 ) ( 54 , 460 ) ( 235 , 316 ) ( 164 , 374 ) ( 331 , 243 ) ( 183 , 434 ) ( 333 , 307 ) ( 259 , 390 ) ( 406 , 259 ) ( 349 , 351 ) ( 256 , 488 ) ( 538 , 95 ) ( 464 , 217 ) ( 379 , 455 ) ( 405 , 403 ) ( 532 , 173 ) ( 517 , 359 ) ( 494 , 491 ) ( 540 , 409 ) ( 563 , 259 ) ( 592 , 274 ) ( 625 , 355 ) ( 656 , 274 ) ( 695 , 368 ) ( 641 , 169 ) ( 733 , 276 ) ( 686 , 88 ) ( 573 , 40 ) List B List (first-->last): ( 573 , 40 ) ( 686 , 88 ) ( 733 , 276 ) ( 695 , 368 ) ( 494 , 491 ) ( 256 , 488 ) ( 54 , 460 ) ( 17 , 264 ) ( 76 , 159 ) ( 174 , 71 ) List (last-->first): ( 174 , 71 ) ( 76 , 159 ) ( 17 , 264 ) ( 54 , 460 ) ( 256 , 488 ) ( 494 , 491 ) ( 695 , 368 ) ( 733 , 276 ) ( 686 , 88 ) ( 573 , 40 ) List A List (first-->last): ( 2 , 0 ) ( 1 , 1 ) ( 4 , 3 ) ( 2 , 2 ) ( 4 , 1 ) ( 3 , 2 ) ( 3 , 4 ) ( 1 , 3 ) ( 2 , 3 ) List (last-->first): ( 2 , 3 ) ( 1 , 3 ) ( 3 , 4 ) ( 3 , 2 ) ( 4 , 1 ) ( 2 , 2 ) ( 4 , 3 ) ( 1 , 1 ) ( 2 , 0 ) List B List (first-->last): ( 2 , 0 ) ( 4 , 1 ) ( 4 , 3 ) ( 3 , 4 ) ( 1 , 3 ) ( 1 , 1 ) List (last-->first): ( 1 , 1 ) ( 1 , 3 ) ( 3 , 4 ) ( 4 , 3 ) ( 4 , 1 ) ( 2 , 0 ) List A List (first-->last): ( 187 , 141 ) ( 247 , 331 ) ( 91 , 315 ) ( 324 , 187 ) ( 80 , 166 ) ( 337 , 308 ) ( 210 , 437 ) List (last-->first): ( 210 , 437 ) ( 337 , 308 ) ( 80 , 166 ) ( 324 , 187 ) ( 91 , 315 ) ( 247 , 331 ) ( 187 , 141 ) List B List (first-->last): ( 187 , 141 ) ( 324 , 187 ) ( 337 , 308 ) ( 210 , 437 ) ( 91 , 315 ) ( 80 , 166 ) List (last-->first): ( 80 , 166 ) ( 91 , 315 ) ( 210 , 437 ) ( 337 , 308 ) ( 324 , 187 ) ( 187 , 141 ) List A List (first-->last): ( 217 , 186 ) ( 284 , 270 ) ( 370 , 377 ) ( 413 , 217 ) ( 195 , 332 ) ( 418 , 458 ) ( 292 , 324 ) List (last-->first): ( 292 , 324 ) ( 418 , 458 ) ( 195 , 332 ) ( 413 , 217 ) ( 370 , 377 ) ( 284 , 270 ) ( 217 , 186 ) List B List (first-->last): ( 217 , 186 ) ( 413 , 217 ) ( 418 , 458 ) ( 195 , 332 ) List (last-->first): ( 195 , 332 ) ( 418 , 458 ) ( 413 , 217 ) ( 217 , 186 )