Advertisement
Alex_tz307

Convex hull trigonometric + coliniare

Dec 17th, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define INF 0x3f3f3f3f
  3. #define pi pair<int,int>
  4. #define mp make_pair
  5. #define x first
  6. #define y second
  7. #define sqr(x) (x) * (x)
  8. #define sz(x) static_cast<int>((x).size())
  9.  
  10. using namespace std;
  11.  
  12. ifstream fin("infasuratoareconvexa.in");
  13. ofstream fout("infasuratoareconvexa.out");
  14.  
  15. int det(pi O, pi A, pi B) {
  16.     return (A.x - O.x) * (B.y - O.y) - (B.x - O.x) * (A.y - O.y);
  17. }
  18.  
  19. int fcmp(const pi &a, const pi &b) {
  20.     int d = det(mp(0, 0), a, b);
  21.     if(d != 0)
  22.         return d > 0;
  23.     return sqr(a.x) + sqr(a.y) > sqr(b.x) + sqr(b.y);
  24. }
  25.  
  26. int main() {
  27.     int N;
  28.     fin >> N;
  29.     vector<pi> a(N + 1);
  30.     a[0] = mp(INF, INF);
  31.     int first = 0;
  32.     for(int i = 1; i <= N; ++i) {
  33.         fin >> a[i].x >> a[i].y;
  34.         if(a[i].y < a[first].y || (a[i].y == a[first].y && a[i].x < a[first].x))
  35.             first = i;
  36.     }
  37.     a[0] = a[first];
  38.     a[first] = a[1];
  39.     a[1] = a[0];
  40.     for(int i = 1; i <= N; ++i) {
  41.         a[i].x -= a[0].x;
  42.         a[i].y -= a[0].y;
  43.     }
  44.     sort(a.begin() + 2, a.end(), fcmp);
  45.     int j;
  46.     for(j = 3; j <= N; ++j)
  47.         if(det(a[1], a[2], a[j]))
  48.             break;
  49.     int i = 2;
  50.     --j;
  51.     while(i < j) {
  52.         swap(a[i], a[j]);
  53.         ++i, --j;
  54.     }
  55.     vector<pi> S;
  56.     S.emplace_back(a[1]), S.emplace_back(a[2]);
  57.     for(int i = 3; i <= N; ++i) {
  58.         while(sz(S) > 1 && det(S[sz(S) - 2], S.back(), a[i]) < 0)
  59.             S.pop_back();
  60.        S.emplace_back(a[i]);
  61.     }
  62.     fout << sz(S) << '\n';
  63.     for(const pi &x : S)
  64.         fout << x.x + a[0].x << ' ' << x.y + a[0].y << '\n';
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement