tuki2501

CONVEXHULL.cpp

Sep 10th, 2020 (edited)
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define point pair<int, int>
  5. #define x first
  6. #define y second
  7.  
  8. bool cross(point o, point a, point b) {
  9.     return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x) <= 0;
  10. }
  11.  
  12. void andrew(vector<point> &a) {
  13.     sort(a.begin(), a.end());
  14.     int n = a.size(), m = 0;
  15.     vector<point> b(n + 1);
  16.     for (int i = 0; i < n; i++) {
  17.         while (m >= 2 && cross(b[m - 2], b[m - 1], a[i])) m--;
  18.         b[m++] = a[i];
  19.     }
  20.     for (int i = n - 2, t = m + 1; i >= 0; i--) {
  21.         while (m >= t && cross(b[m - 2], b[m - 1], a[i])) m--;
  22.         b[m++] = a[i];
  23.     }
  24.     a.resize(1);
  25.     for (int i = 1; i < m - 1; i++) {
  26.         if (b[i] != b[i + 1]) a.push_back(b[i]);
  27.     }
  28. }
  29.  
  30. int main(){
  31. #ifdef _DEBUG
  32.     freopen("in", "r", stdin);
  33. #endif
  34.     ios::sync_with_stdio(0); cin.tie(0);
  35.     int n;
  36.     while (cin >> n, n) {
  37.         vector<point> a(n);
  38.         for (int i = 0; i < n; i++) {
  39.       cin >> a[i].x >> a[i].y;
  40.     }
  41.         andrew(a);
  42.         cout << a.size() << '\n';
  43.         for (int i = 0; i < a.size(); i++) {
  44.             cout << a[i].x << ' ' << a[i].y << '\n';
  45.     }
  46.     }
  47. }
  48.  
Add Comment
Please, Sign In to add comment