AlexNeagu11

Infasuratoare Convexa

Mar 19th, 2022
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4. struct point {
  5. int x, y;
  6. void read() {
  7. cin >> x >> y;
  8. }
  9. void write() {
  10. cout << x << ' ' << y << '\n';
  11. }
  12. point operator - (const point &B) const {
  13. return point{x - B.x, y - B.y};
  14. }
  15. void operator -= (const point &B) {
  16. x -= B.x;
  17. y -= B.y;
  18. }
  19. int operator * (const point &B) const {
  20. return x * B.y - y * B.x;
  21. }
  22. int orientation(const point &A, const point &B) const {
  23. return (A - *this) * (B - *this);
  24. }
  25. int dist(point A) {
  26. A -= *this;
  27. return A.x * A.x + A.y * A.y;
  28. }
  29. bool operator <(const point &A) {
  30. if(x == A.x) {
  31. return y < A.y;
  32. }
  33. return x < A.x;
  34. }
  35.  
  36. };
  37. vector<point> buildHull(vector<point> A) {
  38. //
  39. vector<point> hull;
  40. for(int i = 0; i < (int) A.size(); ++i) {
  41. while(hull.size() >= 2) {
  42. int sz = hull.size();
  43. point P1 = hull[sz - 2];
  44. point P2 = hull[sz - 1];
  45. if(P2.orientation(P1, A[i]) <= 0) {
  46. break;
  47. }
  48. hull.pop_back();
  49. }
  50. hull.push_back(A[i]);
  51. }
  52. return hull;
  53. }
  54.  
  55. int32_t main() {
  56.  
  57. int n;
  58. cin >> n;
  59. vector<point> points(n);
  60. for(int i = 0; i < n; ++i) {
  61. points[i].read();
  62. }
  63.  
  64. sort(points.begin(), points.end());
  65. vector<point> upperHull = buildHull(points);
  66. reverse(points.begin(), points.end());
  67. vector<point> lowerHull = buildHull(points);
  68.  
  69. upperHull.pop_back();
  70. lowerHull.pop_back();
  71. cout << upperHull.size() + lowerHull.size() << '\n';
  72.  
  73. for(auto it : upperHull) {
  74. it.write();
  75. }
  76. for(auto it : lowerHull) {
  77. it.write();
  78. }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment