AlexNeagu11

InfasuratoareArena

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