Advertisement
Tooster

otoczka 2

Jan 6th, 2016
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. #include <cstdio>
  2. #include <functional>
  3. #include <algorithm>
  4. #include <math.h>
  5. #include <queue>
  6.  
  7. using namespace std;
  8.  
  9. struct point {
  10. int x;
  11. int y;
  12. };
  13. point P1;
  14. point *tab;
  15.  
  16. int iloczyn(point a, point b, point c) {
  17. int area = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
  18. return area; //if >0 c is on the left of AB
  19. }
  20.  
  21. int dist(point O, point P) {
  22. int x = P.x - O.x; int y = P.y - O.y;
  23. return x*x + y*y;
  24. }
  25.  
  26. bool comp(point a, point b) {
  27. int dot_product = iloczyn(tab[0], a, b);
  28. return (dot_product > 0 || (dot_product == 0 && dist(tab[0], a) < dist(tab[0], b)));
  29. }
  30.  
  31. int main() {
  32. int n; scanf("%d", &n);
  33.  
  34. tab = new point[n];
  35.  
  36. int x, y; scanf("%d %d", &x, &y); tab[0].x = x; tab[0].y = y;
  37.  
  38. //////////////////////////////////////
  39. for (int i = 1; i < n; i++) {
  40. scanf("%d %d", &x, &y);
  41. tab[i].x = x; tab[i].y = y;
  42. if (tab[i].y < tab[0].y || (tab[i].y == tab[0].y && tab[i].x < tab[0].x)) {
  43. swap(tab[i], tab[0]);
  44. }
  45. }
  46.  
  47. sort(tab + 1, tab + n, comp);
  48.  
  49. vector<point> otoczka;
  50. otoczka.push_back(tab[0]);
  51. otoczka.push_back(tab[1]);
  52.  
  53. for (int i = 2; i < n; i++) {
  54. point B = otoczka.back();
  55. otoczka.pop_back();
  56. while (otoczka.size() > 0 && iloczyn(otoczka.back(), B, tab[i]) <= 0) {
  57. B = otoczka.back();
  58. otoczka.pop_back();
  59. }
  60. otoczka.push_back(B);
  61. otoczka.push_back(tab[i]);
  62. }
  63. otoczka.push_back(tab[0]);
  64. //////////////////////////////////////
  65.  
  66. while (!otoczka.empty()) {
  67. printf("%d %d\n", otoczka.back().x, otoczka.back().y);
  68. otoczka.pop_back();
  69. }
  70. return 0;
  71. }
  72. //@author Tooster
  73. /*
  74. 5
  75. 1 1
  76. 1 2
  77. 2 3
  78. 3 2
  79. 2 1
  80.  
  81. 6
  82. 1 1
  83. 2 3
  84. 3 5
  85. 4 1
  86. 2 1
  87. 3 2
  88. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement