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

#include <bits/stdc++.h>
using namespace std;

typedef struct point {
  int x, y;
  point operator+(const point &a) {
    point p = (point){x + a.x, y + a.y};
    return p;
  }
  point operator-(const point &a) {
    point p = (point){x - a.x, y - a.y};
    return p;
  }
} point;

bool grid[5001][5001];

int calc(point u, point v) {
  point a = u - v;
  return a.x * a.x + a.y * a.y;
}

void rotate(point &u) {
  int tmp = u.x;
  u.x = u.y, u.y = -tmp;
}

bool range(int y, int x) {
  return (0 <= x and x < 5000 and 0 <= y and y < 5000);
}

int main() {
  int n;
  while (std::cin >> n, n) {
    memset(grid, false, sizeof(grid));
    vector<point> vp(n);
    int x, y;
    for (int i = 0; i < n; i++) {
      std::cin >> x >> y;
      grid[y][x] = true;
      vp[i] = (point){x, y};
    }
    int area = 0;
    for (int i = 0; i < n; i++) {
      point u = vp[i];
      for (int j = 0; j < n; j++) {
        if (i == j)
          continue;
        point v = vp[j];
        point uv = v - u;
        // std::cout << "uv.x:" << uv.x << " uv.y:" << uv.y << std::endl;
        rotate(uv);
        // std::cout << "uv.x:" << uv.x << " uv.y:" << uv.y << std::endl;

        point x = v + uv, y = u + uv;
        if (range(x.y, x.x) and range(y.y, y.x) and grid[x.y][x.x] == true and
            grid[y.y][y.x] == true and area < calc(u, v)) {
          area = calc(u, v);
          // std::cout << "u.x:" << u.x << " u.y:" << u.y << std::endl;
        }
        rotate(uv);
        rotate(uv);
        x = v + uv, y = u + uv;
        if (range(x.y, x.x) and range(y.y, y.x) and grid[x.y][x.x] == true and
            grid[y.y][y.x] == true and area < calc(u, v))
          area = calc(u, v);
      }
    }
    std::cout << area << std::endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <utility>
using namespace std;
int grid[5001][5001];
struct cord {
  int x, y;
  cord operator+(const cord &a) const {
    cord res;
    res.x = a.x + x;
    res.y = a.y + y;
    return res;
  }
  cord operator-(const cord &a) const {
    cord res;
    res.x = a.x - x;
    res.y = a.y - y;
    return res;
  }
  bool operator<=(const cord &a) const {
    if (a.x <= x && a.y <= y)
      return true;
    return false;
  }
  ~cord() {}
};

void rev(cord &a) {
  int tmp;
  tmp = a.x;
  a.x = a.y;
  a.y = -tmp;
}

int main() {
  int n;
  while (cin >> n, n) {
    memset(grid, 0, sizeof(grid));
    int x[n], y[n];
    for (int i = 0; i < n; i++) {
      cin >> x[i] >> y[i];
      grid[y[i]][x[i]] = 1;
    }
    int ans = 0;
    cord a, b, d, emp;
    for (int i = 0; i < n; i++) {
      a.x = x[i];
      a.y = y[i];
      for (int j = 0; j < n && j != i; j++) {
        b.x = x[j];
        b.y = y[j];
        emp.x = 0;
        emp.y = 0;
        d = a - b;
        rev(d);
        // cout << "d.x:" << d.x  << " d.y:" << d.y<< endl;
        // cout << "i:" << i << " j:" << j << endl;
        if (emp <= a + d && emp <= b + d && grid[a.y + d.y][a.x + d.x] == 1 &&
            grid[b.y + d.y][b.x + d.x] == 1) {
          cord tmp = a - b;
          ans = max(ans, tmp.x * tmp.x + tmp.y * tmp.y);
          // cout << "ans:" << ans<< endl;
        }

        if (emp <= a - d && emp <= b - d && grid[a.y - d.y][a.x - d.x] == 1 &&
            grid[b.y - d.y][b.x - d.x] == 1) {
          cord tmp = a - b;
          ans = max(ans, tmp.x * tmp.x + tmp.y * tmp.y);
          // cout << "ans:" << ans<< endl;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}