fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <sstream>
  5.  
  6. using namespace std;
  7.  
  8. struct Point
  9. {
  10. int x,y;
  11. friend std::ostream &operator << (std::ostream &output,const Point &p);
  12. friend std::istream &operator >> (std::istream &input,Point &p);
  13. };
  14.  
  15. bool operator == (Point a,Point b)
  16. {
  17. return (a.x == b.x && a.y == b.y);
  18. }
  19.  
  20. std::ostream &operator << (std::ostream &output,const Point &p)
  21. {
  22. output<<"( "<<p.x<<" , "<<p.y<<" ) ";
  23. return output;
  24. }
  25.  
  26.  
  27.  
  28. std::istream &operator >> (std::istream &input,Point &p)
  29. {
  30. input>>p.x>>p.y;;
  31. return input;
  32. }
  33.  
  34.  
  35. template<class T>
  36. class Link
  37. {
  38. private:
  39. T data;
  40. Link<T> *next;
  41. Link<T> *previous;
  42. public:
  43. Link(T data);
  44. void displayLink();
  45. T getData();
  46. void setData(T data);
  47. Link<T> *getNext();
  48. void setNext(Link<T> *next);
  49. Link<T> *getPrevious();
  50. void setPrevious(Link<T> *previous);
  51. template<class U> friend std::ostream& operator<< (std::ostream& print, Link<U>& obj);
  52.  
  53. };
  54.  
  55. template<class T>
  56. Link<T>::Link(T data)
  57. {
  58. setData(data);
  59. setNext(NULL);
  60. setPrevious(NULL);
  61.  
  62. }
  63.  
  64. template<class T>
  65. void Link<T>::displayLink()
  66. {
  67. std::cout<<getData()<<" ";
  68. }
  69.  
  70. template<class T>
  71. T Link<T>::getData()
  72. {
  73. return data;
  74. }
  75.  
  76. template<class T>
  77. void Link<T>::setData(T data)
  78. {
  79. this->data = data;
  80. }
  81.  
  82. template<class T>
  83. Link<T> *Link<T>::getNext()
  84. {
  85. return next;
  86. }
  87.  
  88. template<class T>
  89. void Link<T>::setNext(Link<T> *next)
  90. {
  91. this->next = next;
  92. }
  93.  
  94. template<class T>
  95. Link<T> *Link<T>::getPrevious()
  96. {
  97. return previous;
  98. }
  99.  
  100. template<class T>
  101. void Link<T>::setPrevious(Link<T> *previous)
  102. {
  103. this->previous = previous;
  104. }
  105.  
  106.  
  107. template <class U>
  108. std::ostream& operator<<(std::ostream& print, Link<U> & L)
  109. {
  110. print << L.getData();
  111. return print;
  112. }
  113.  
  114. template<class T>
  115. class DoublyLinkedList
  116. {
  117. private:
  118. Link<T> *first;
  119. Link<T> *last;
  120. public:
  121. DoublyLinkedList();
  122. ~DoublyLinkedList();
  123. Link<T> *getFirst();
  124. void setFirst(Link<T> *first);
  125. Link<T> *getLast();
  126. void setLast(Link<T> *first);
  127. bool isEmpty();
  128. Link<T> *find(T key);
  129. void insertFirst(T dd);
  130. void insertLast(T dd);
  131. void deleteFirst();
  132. void deleteLast();
  133. bool insertAfter(T key,T dd);
  134. void deleteKey(T key);
  135. void displayForward();
  136. void displayBackward();
  137. };
  138.  
  139. template<class T>
  140. DoublyLinkedList<T>::DoublyLinkedList()
  141. {
  142. setFirst(NULL);
  143. setLast(NULL);
  144. }
  145.  
  146. template<class T>
  147. DoublyLinkedList<T>::~DoublyLinkedList()
  148. {
  149. while(!isEmpty())
  150. {
  151. deleteFirst();
  152. }
  153. }
  154.  
  155. template<class T>
  156. Link<T> *DoublyLinkedList<T>::getFirst()
  157. {
  158. return first;
  159. }
  160.  
  161. template<class T>
  162. void DoublyLinkedList<T>::setFirst(Link<T> *first)
  163. {
  164. this->first = first;
  165. }
  166.  
  167. template<class T>
  168. Link<T> *DoublyLinkedList<T>::getLast()
  169. {
  170. return last;
  171. }
  172.  
  173. template<class T>
  174. void DoublyLinkedList<T>::setLast(Link<T> *last)
  175. {
  176. this->last = last;
  177. }
  178.  
  179. template<class T>
  180. bool DoublyLinkedList<T>::isEmpty()
  181. {
  182. return getFirst()==NULL;
  183. }
  184.  
  185. template<class T>
  186. Link<T> *DoublyLinkedList<T>::find(T key)
  187. {
  188. Link<T> *current = getFirst();
  189. while(current != NULL && !(current->getData == key))
  190. current = current->getNext();
  191. return current;
  192. }
  193.  
  194. template<class T>
  195. void DoublyLinkedList<T>::insertFirst(T dd)
  196. {
  197. Link<T> *newLink = new Link<T>(dd);
  198. if(isEmpty())
  199. setLast(newLink);
  200. else
  201. getFirst()->setPrevious(newLink);
  202. newLink->setNext(getFirst());
  203. setFirst(newLink);
  204. }
  205.  
  206. template<class T>
  207. void DoublyLinkedList<T>::insertLast(T dd)
  208. {
  209. Link<T> *newLink = new Link<T>(dd);
  210. if(isEmpty())
  211. {
  212. setFirst(newLink);
  213. }
  214. else
  215. {
  216. getLast()->setNext(newLink);
  217. newLink->setPrevious(getLast());
  218. }
  219. setLast(newLink);
  220. }
  221.  
  222. template<class T>
  223. void DoublyLinkedList<T>::deleteFirst()
  224. {
  225. if(isEmpty())
  226. return;
  227. Link<T> *temp = getFirst();
  228. if(getFirst()->getNext() == NULL)
  229. setLast(NULL);
  230. else
  231. getFirst()->getNext()->setPrevious(NULL);
  232. setFirst(getFirst()->getNext());
  233. delete temp;
  234. }
  235.  
  236. template<class T>
  237. void DoublyLinkedList<T>::deleteLast()
  238. {
  239. if(isEmpty())
  240. return;
  241. Link<T> *temp = getLast();
  242. if(getFirst()->getNext() == NULL)
  243. setFirst(NULL);
  244. else
  245. getLast()->getPrevious()->setNext(NULL);
  246. setLast(getLast()->getPrevious());
  247. delete temp;
  248. }
  249.  
  250. template<class T>
  251. bool DoublyLinkedList<T>::insertAfter(T key,T dd)
  252. {
  253. Link<T> *current = find(key);
  254. if(current == NULL)
  255. return false;
  256. Link<T> *newLink = new Link<T>(dd);
  257. if(current->getNext() == NULL)
  258. {
  259. newLink->setNext(NULL);
  260. setLast(newLink);
  261. }
  262. else
  263. {
  264. newLink->setNext(current->getNext());
  265. current->getNext()->setPrevious(newLink);
  266. }
  267. newLink->setPrevious(current);
  268. current->setNext(newLink);
  269. return true;
  270. }
  271.  
  272. template<class T>
  273. void DoublyLinkedList<T>::deleteKey(T key)
  274. {
  275. Link<T> *current = find(key);
  276. if(current==NULL)
  277. return;
  278. if(current->getPrevious() == NULL)
  279. setFirst(current->getNext());
  280. else
  281. current->getPrevious()->setNext(current->getNext());
  282. if(current->getNext() == NULL)
  283. setLast(current->getPrevious());
  284. else
  285. current->getNext()->setPrevious(current->getPrevious());
  286. delete current;
  287. }
  288.  
  289. template<class T>
  290. void DoublyLinkedList<T>::displayForward()
  291. {
  292. std::cout<<"List (first-->last): ";
  293. Link<T> *current = getFirst();
  294. while(current != NULL)
  295. {
  296. current->displayLink();
  297. current = current->getNext();
  298. }
  299. std::cout<<"\n";
  300. }
  301.  
  302. template<class T>
  303. void DoublyLinkedList<T>::displayBackward()
  304. {
  305. std::cout<<"List (last-->first): ";
  306. Link<T> *current = getLast();
  307. while(current != NULL)
  308. {
  309. current->displayLink();
  310. current = current->getPrevious();
  311. }
  312. std::cout<<"\n";
  313. }
  314.  
  315.  
  316.  
  317. int vect(Point a1,Point a2,Point b1,Point b2)
  318. {
  319. return (a2.x - a1.x)*(b2.y - b1.y) - (b2.x - b1.x)*(a2.y - a1.y);
  320. }
  321.  
  322. int dist2(Point a1,Point a2)
  323. {
  324. return (a2.x - a1.x)*(a2.x - a1.x) + (a2.y - a1.y)*(a2.y - a1.y);
  325. }
  326.  
  327. void Solve(DoublyLinkedList<Point> &A,DoublyLinkedList<Point> &B)
  328. {
  329. Link<Point> *p,*q,*m,*min;
  330. if(!A.isEmpty())
  331. {
  332. m = A.getFirst();
  333. p = m->getNext();
  334. while(p != NULL)
  335. {
  336. if(p->getData().y < m->getData().y)
  337. m = p;
  338. else if(p->getData().y == m->getData().y && p->getData().x > m->getData().x)
  339. m = p;
  340. p = p->getNext();
  341. }
  342.  
  343. if(m->getPrevious() == NULL)
  344. A.setFirst(m->getNext());
  345. else
  346. m->getPrevious()->setNext(m->getNext());
  347. if(m->getNext() == NULL)
  348. A.setLast(m->getPrevious());
  349. else
  350. m->getNext()->setPrevious(m->getPrevious());
  351.  
  352. if(A.isEmpty())
  353. A.setLast(m);
  354. else
  355. A.getFirst()->setPrevious(m);
  356. m->setNext(A.getFirst());
  357. m->setPrevious(NULL);
  358. A.setFirst(m);
  359.  
  360. B.insertLast(A.getFirst()->getData());
  361.  
  362. if(A.getFirst()->getNext() != NULL)
  363. min = A.getFirst()->getNext();
  364. else
  365. min = A.getFirst();
  366.  
  367. do
  368. {
  369. q = A.getFirst()->getNext();
  370. while(q != NULL)
  371. {
  372. if((vect(B.getLast()->getData(),min->getData(),B.getLast()->getData(),q->getData()) < 0)||
  373. (vect(B.getLast()->getData(),min->getData(),B.getLast()->getData(),q->getData()) == 0)&&
  374. (dist2(B.getLast()->getData(),min->getData()) < dist2(B.getLast()->getData(),q->getData())))
  375. min = q;
  376. q = q->getNext();
  377. }
  378. B.insertLast(min->getData());
  379. min = A.getFirst();
  380. }
  381. while(!(B.getLast()->getData() == B.getFirst()->getData()));
  382. B.deleteLast();
  383. }
  384. }
  385.  
  386. int main(int argc, char *argv[])
  387. {
  388. DoublyLinkedList<Point> A;
  389. DoublyLinkedList<Point> B;
  390. stringstream ss;
  391. Point p;
  392. string line;
  393. int counter;
  394. while(getline(cin,line))
  395. {
  396. ss.clear();
  397. ss.str(line);
  398. counter = 1;
  399. while(ss)
  400. {
  401. try
  402. {
  403. switch(counter)
  404. {
  405. case 1:
  406. {
  407. ss>>p.x;
  408. break;
  409. }
  410. case 0:
  411. {
  412. ss>>p.y;
  413. A.insertLast(p);
  414. break;
  415. }
  416. }
  417. }
  418. catch(...)
  419. {
  420.  
  421. }
  422. counter = (counter + 1)%2;
  423. }
  424. Solve(A,B);
  425. cout<<"List A \n";
  426. A.displayForward();
  427. A.displayBackward();
  428. cout<<"List B \n";
  429. B.displayForward();
  430. B.displayBackward();
  431. while(!A.isEmpty())
  432. A.deleteFirst();
  433. while(!B.isEmpty())
  434. B.deleteFirst();
  435. }
  436. return EXIT_SUCCESS;
  437. }
  438.  
Success #stdin #stdout 0s 15240KB
stdin
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
stdout
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 )