Problem p00441 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
int sign(const float &d) { return (d == 0) ? 0 : (d > 0) ? 1 : -1; }
struct Gradient {
  float gradient{0};
  Gradient(){};
  Gradient(const float diffx, const float diffy)
      : gradient{(sign(diffx) * diffy > 0) ? (diffx / diffy)
                                           : std::abs(diffy / diffx)} {}
  bool operator>(const Gradient g) const { return gradient > g.gradient; }
  bool operator<(const Gradient g) const { return gradient < g.gradient; }
  bool operator==(const Gradient g) const { return gradient == g.gradient; }
};
struct Point {
  float x{0}, y{0};
  Point(){};
  Point(const float ax, const float ay) : x{ax}, y{ay} {}
  bool operator<(const Point &other) const {
    return (x == other.x) ? y < other.y : x < other.x;
  }
  bool operator>(const Point &other) const {
    return (x == other.x) ? y > other.y : x > other.x;
  }
  bool operator==(const Point &other) const {
    return x == other.x && y == other.y;
  }
};
class MidPoint {
private:
  float length{};
  Point mid_point{};
  Gradient gradient{};

public:
  MidPoint() {}
  MidPoint(const float lengthSquare, const Point &mid, const Gradient &g)
      : length{lengthSquare}, mid_point{mid}, gradient{g} {}
  bool operator>(const MidPoint &other) const {
    if (length == other.length) {
      if (mid_point == other.mid_point)
        return gradient > other.gradient;
      else
        return mid_point > other.mid_point;
    } else
      return length > other.length;
  }
  bool operator<(const MidPoint &other) const {
    if (length == other.length) {
      if (mid_point == other.mid_point)
        return gradient < other.gradient;
      else
        return mid_point < other.mid_point;
    } else
      return length < other.length;
  }
  bool operator==(const MidPoint &other) const {
    return length == other.length && mid_point == other.mid_point &&
           gradient == other.gradient;
  }
  int square_size() const { return static_cast<int>(length) / 2; }
};
MidPoint midPointBetween(const Point &a, const Point &b) {
  return MidPoint((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y),
                  Point{(a.x + b.x) / 2, (a.y + b.y) / 2},
                  Gradient(a.x - b.x, a.y - b.y));
}

std::vector<int> solve(int n) {
  std::vector<int> res;
  while (n != 0) {
    std::vector<Point> pilars(n);
    for (auto &pilar : pilars)
      std::cin >> pilar.x >> pilar.y;
    std::vector<MidPoint> mid_points(n * (n - 1) / 2);
    int index{0};
    for (auto i = 0; i < n; ++i) {
      for (auto j = i + 1; j < n; ++j) {
        mid_points[index++] = midPointBetween(pilars[i], pilars[j]);
      }
    }
    std::sort(mid_points.begin(), mid_points.end());
    --index;
    for (; index > 0; --index) {
      if (mid_points[index] == mid_points[index - 1])
        break;
    }
    if (index != 0)
      res.push_back(mid_points[index].square_size());
    else
      res.push_back(0);
    std::cin >> n;
  }
  return res;
}
int main() {
  int n;
  std::cin >> n;
  auto res = solve(n);
  for (auto r : res)
    std::cout << r << std::endl;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
int sign(const double &d) { return (d == 0) ? 0 : (d > 0) ? 1 : -1; }
struct Gradient {
  double gradient{0};
  Gradient(){};
  Gradient(const double diffx, const double diffy)
      : gradient{(sign(diffx) * diffy > 0) ? (diffx / diffy)
                                           : std::abs(diffy / diffx)} {}
  bool operator>(const Gradient g) const { return gradient > g.gradient; }
  bool operator<(const Gradient g) const { return gradient < g.gradient; }
  bool operator==(const Gradient g) const { return gradient == g.gradient; }
};
struct Point {
  double x{0}, y{0};
  Point(){};
  Point(const double ax, const double ay) : x{ax}, y{ay} {}
  bool operator<(const Point &other) const {
    return (x == other.x) ? y < other.y : x < other.x;
  }
  bool operator>(const Point &other) const {
    return (x == other.x) ? y > other.y : x > other.x;
  }
  bool operator==(const Point &other) const {
    return x == other.x && y == other.y;
  }
};
class MidPoint {
private:
  double length{};
  Point mid_point{};
  Gradient gradient{};

public:
  MidPoint() {}
  MidPoint(const double lengthSquare, const Point &mid, const Gradient &g)
      : length{lengthSquare}, mid_point{mid}, gradient{g} {}
  bool operator>(const MidPoint &other) const {
    if (length == other.length) {
      if (mid_point == other.mid_point)
        return gradient > other.gradient;
      else
        return mid_point > other.mid_point;
    } else
      return length > other.length;
  }
  bool operator<(const MidPoint &other) const {
    if (length == other.length) {
      if (mid_point == other.mid_point)
        return gradient < other.gradient;
      else
        return mid_point < other.mid_point;
    } else
      return length < other.length;
  }
  bool operator==(const MidPoint &other) const {
    return length == other.length && mid_point == other.mid_point &&
           gradient == other.gradient;
  }
  int square_size() const { return static_cast<int>(length) / 2; }
};
MidPoint midPointBetween(const Point &a, const Point &b) {
  return MidPoint((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y),
                  Point{(a.x + b.x) / 2, (a.y + b.y) / 2},
                  Gradient(a.x - b.x, a.y - b.y));
}

std::vector<int> solve(int n) {
  std::vector<int> res;
  while (n != 0) {
    std::vector<Point> pilars(n);
    for (auto &pilar : pilars)
      std::cin >> pilar.x >> pilar.y;
    std::vector<MidPoint> mid_points(n * (n - 1) / 2);
    int index{0};
    for (auto i = 0; i < n; ++i) {
      for (auto j = i + 1; j < n; ++j) {
        mid_points[index++] = midPointBetween(pilars[i], pilars[j]);
      }
    }
    std::sort(mid_points.begin(), mid_points.end());
    --index;
    for (; index > 0; --index) {
      if (mid_points[index] == mid_points[index - 1])
        break;
    }
    if (index != 0)
      res.push_back(mid_points[index].square_size());
    else
      res.push_back(0);
    std::cin >> n;
  }
  return res;
}
int main() {
  int n;
  std::cin >> n;
  auto res = solve(n);
  for (auto r : res)
    std::cout << r << std::endl;
}