Advertisement
double_trouble

Convex Hull

Feb 26th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <vector>
  5. #include <cmath>
  6. #include <string>
  7. #include <algorithm>
  8. #include <string>
  9. #include <deque>
  10. #include <iomanip>
  11. #include <cstddef>
  12.  
  13. #define F first
  14. #define S second
  15.  
  16. using namespace std;
  17.  
  18. struct point
  19. {
  20.     double x, y;
  21.     point():x(0.0), y(0.0){}
  22.     point(double _x, double _y):x(_x), y(_y){}
  23. };
  24.  
  25. point operator+(point a, point b)
  26. {
  27.     return point(a.x + b.x, a.y + b.y);
  28. }
  29.  
  30. point operator-(point a, point b)
  31. {
  32.     return point(a.x - b.x, a.y - b.y);
  33. }
  34.  
  35. point operator*(point a, double k)
  36. {
  37.     return point(a.x * k, b.y * k);
  38. }
  39.  
  40. point operator/(point a, double k)
  41. {
  42.     return point(a.x / k, b.y / k);
  43. }
  44.  
  45. double vect(point a, point b)
  46. {
  47.     return a.x * b.y - a.y * b.x;
  48. }
  49.  
  50. const long double eps = 0.000000005;
  51. const long double pi = 3.1415926535897932;
  52. const long long inf = 1e9;
  53.  
  54. vector <point> all, up, down;
  55.  
  56. bool comp(const point& a, const point& b)
  57. {
  58.     return (a.x < b.x) || (a.x == b.x && a.y < b.y);
  59. }
  60.  
  61. int main()
  62. {
  63.     ios_base::sync_with_stdio(0);
  64.     //freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
  65.     // freopen("slalom.in", "r", stdin);freopen("slalom.out", "w", stdout);
  66.  
  67.     int n;
  68.     cin >> n;
  69.     double x, y;
  70.     for (int i = 0; i < n; i++)
  71.     {
  72.         cin >> x >> y;
  73.         all.push_back(point(x, y));
  74.     }
  75.  
  76.     sort(all.begin(), all.end(), comp);
  77.  
  78.     point b = all[0], e = all.back();
  79.  
  80.     up.push_back(b);
  81.     down.push_back(b);
  82.  
  83.     for (int i = 1; i < all.size(); i++)
  84.     {
  85.         point t = all[i];
  86.         if (vect(e - b, t - b) > eps || i == all.size() - 1)
  87.         {
  88.             while (up.size() > 1 && vect(t - up.back(), up[up.size() - 2] - up.back()) > -eps)
  89.                 up.pop_back();
  90.             up.push_back(t);
  91.         }
  92.         if (vect(e - b, t - b) < -eps || i == all.size() - 1)
  93.         {
  94.             while (down.size() > 1 && vect(t - down.back(), down[down.size() - 1] - down.back()) < eps)
  95.                 down.pop_back();
  96.             down.push_back(t);
  97.         }
  98.     }
  99.  
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement