Advertisement
tien_noob

Convex Hull - Graham Scan

Oct 28th, 2021
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. //Make CSP great again
  2. //You're as beautiful as the day I lost you
  3. #include <bits/stdc++.h>
  4. #define TASK "CONVEXHULL"
  5. #define Log2(x) 31 - __builtin_clz(x)
  6. using namespace std;
  7. const int N = 1e5;
  8. int n;
  9. struct TPoint
  10. {
  11.     long long x, y;
  12. };
  13. using TVector = TPoint;
  14. TVector operator - (const TVector & a, const TVector & b)
  15. {
  16.     return {b.x - a.x, b.y - a.y};
  17. }
  18. long long operator * (const TVector & a, const TVector & b)
  19. {
  20.     return a.x * b.y - a.y * b.x;
  21. }
  22. long long distance(TPoint a, TPoint b)
  23. {
  24.     return (b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y);
  25. }
  26. TPoint p[N + 1];
  27. TVector convex[N + 1];
  28. void read()
  29. {
  30.     cin >> n;
  31.     for (int i = 0; i < n; ++ i)
  32.     {
  33.         cin >> p[i].x >> p[i].y;
  34.     }
  35.     swap(*min_element(p, p + n, [](const TPoint & a, const TPoint & b)
  36.                       {
  37.                           if (a.y != b.y)
  38.                           {
  39.                               return a.y < b.y;
  40.                           }
  41.                           else
  42.                           {
  43.                               return a.x < b.x;
  44.                           }
  45.                       }), p[0]);
  46.     sort(p + 1, p + n, [](const TPoint & a, const TPoint & b)
  47.          {
  48.              long long tmp = (a - p[0]) * (b - p[0]);
  49.              if (tmp != 0)
  50.              {
  51.                  return tmp > 0;
  52.              }
  53.              return distance(a, p[0]) < distance(p[0], b);
  54.          });
  55. }
  56. bool TurnLeft(TVector & a, TVector & b, TVector & c)
  57. {
  58.     return (b - a) * (c - b) > 0;
  59. }
  60. void solve()
  61. {
  62.     int m = 0;
  63.     for (int i = 0; i < n; ++ i)
  64.     {
  65.         while(m >= 2 && !TurnLeft(convex[m - 1], convex[m], p[i]))
  66.         {
  67.             --m;
  68.         }
  69.         ++m;
  70.         convex[m] = p[i];
  71.     }
  72.     cout << m << '\n';
  73.     long long s = 0;
  74.     TVector O = {0, 0};
  75.     convex[m + 1] = convex[1];
  76.     for (int i = 1; i <= m; ++ i)
  77.     {
  78.         s += convex[i] * convex[i + 1];
  79.     }
  80.     cout << fixed << setprecision(1) << (long double)s/ 2 << '\n';
  81.     for (int i = 1; i <= m; ++ i)
  82.     {
  83.         cout << convex[i].x << ' ' << convex[i].y << '\n';
  84.     }
  85. }
  86. int main()
  87. {
  88.     ios_base::sync_with_stdio(false);
  89.     cin.tie(nullptr);
  90.     if (fopen(TASK".INP", "r"))
  91.     {
  92.         freopen(TASK".INP", "r", stdin);
  93.         freopen(TASK".OUT", "w", stdout);
  94.     }
  95.     int t = 1;
  96.     bool typetest = false;
  97.     if (typetest)
  98.     {
  99.         cin >> t;
  100.     }
  101.     for (int __ = 1; __ <= t; ++ __)
  102.     {
  103.         //cout << "Case " << __ << ": ";
  104.         read();
  105.         solve();
  106.     }
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement