Advertisement
Alex_tz307

Convex Hull

Dec 17th, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define sz(x) static_cast<int>((x).size())
  3. #define pd pair<double,double>
  4. #define x first
  5. #define y second
  6.  
  7. using namespace std;
  8.  
  9. ifstream fin("infasuratoare.in");
  10. ofstream fout("infasuratoare.out");
  11.  
  12. const double EPS = 1e-12;
  13.  
  14. bool check(pd O, pd A, pd B) {
  15.     return ((A.x - O.x) * (B.y - O.y) - (B.x - O.x) * (A.y - O.y)) < EPS;
  16. }
  17.  
  18. int main() {
  19.     int N;
  20.     fin >> N;
  21.     vector<pd> a(N);
  22.     for(pd &x : a)
  23.         fin >> x.x >> x.y;
  24.     sort(a.begin(), a.end());
  25.     vector<int> S;
  26.     S.emplace_back(0), S.emplace_back(1);
  27.     vector<bool> viz(N);
  28.     viz[1] = true;
  29.     for(int i = 0, add = 1; i >= 0; i += (add = (i == N - 1 ? -add : add))) {
  30.         if(viz[i])
  31.             continue;
  32.         while(sz(S) > 1 && check(a[S[sz(S) - 2]], a[S.back()], a[i])) {
  33.             viz[S.back()] = false;
  34.             S.pop_back();
  35.         }
  36.         S.emplace_back(i);
  37.         viz[i] = true;
  38.     }
  39.     fout << sz(S) - 1 << '\n' << fixed << setprecision(12);
  40.     for(int i = 0; i < sz(S) - 1; ++i)
  41.         fout << a[S[i]].x << ' ' << a[S[i]].y << '\n';
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement