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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, j) FOR(i, 0, j)

struct P {
  int x, y;

  double size() { return std::sqrt(this->x * this->x + this->y * this->y); }

  P operator+(const P &p) const { return P{this->x + p.x, this->y + p.y}; }
  P operator-(const P &p) const { return P{this->x - p.x, this->y - p.y}; }
};

std::vector<P> pillars;
int isUsed[3000][3000];

bool ascending_P(const P &lp, const P &rp) {
  if (lp.x != rp.x) {
    return lp.x < rp.x;
  }
  return lp.y < rp.y;
}

P rotate_90(P p) { return P{-p.y, p.x}; }

bool isWithinRange(P p) {
  return 0 <= p.x && p.x <= 5000 && 0 <= p.y && p.y <= 5000;
}

bool canCreateSite(P p, P v) {
  REP(i, 4) {
    if (isWithinRange(p) &&
        std::binary_search(pillars.begin(), pillars.end(), p, ascending_P)) {
      p = p + v;
      v = rotate_90(v);
    } else {
      return false;
    }
  }

  return true;
}

int main() {
  int n;
  while (std::cin >> n, n) {
    pillars.clear();

    REP(i, n) {
      REP(j, n) { isUsed[i][j] = 0; }
    }

    REP(i, n) {
      int x, y;
      std::cin >> x >> y;
      pillars.push_back(P{x, y});
    }

    std::sort(pillars.begin(), pillars.end(), ascending_P);

    int res = 0;
    REP(i, n) {
      FOR(j, i + 1, n) {
        if (isUsed[i][j] || isUsed[j][i]) {
          continue;
        }

        isUsed[i][j] = 1;
        P a = pillars[i], b = pillars[j], v = b - a;
        int S = v.x * v.x + v.y * v.y;

        if (canCreateSite(a, b - a)) {
          res = std::max(S, res);
        } else if (canCreateSite(b, a - b)) {
          res = std::max(S, res);
        }
      }
    }

    std::cout << res << std::endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, j) FOR(i, 0, j)

struct P {
  int x, y;

  double size() { return std::sqrt(this->x * this->x + this->y * this->y); }

  P operator+(const P &p) { return P{this->x + p.x, this->y + p.y}; }
  P operator-(const P &p) { return P{this->x - p.x, this->y - p.y}; }
};

std::vector<P> pillars;
int map[5001][5001], isUsed[3000][3000];

P rotate_90(P p) { return P{-p.y, p.x}; }

bool isWithinRange(P p) {
  return 0 <= p.x && p.x <= 5000 && 0 <= p.y && p.y <= 5000;
}

bool canCreateSite(P p, P v) {
  REP(i, 4) {
    if (isWithinRange(p) && map[p.x][p.y]) {
      p = p + v;
      v = rotate_90(v);
    } else {
      return false;
    }
  }

  return true;
}

int main() {
  int n;
  while (std::cin >> n, n) {
    pillars.clear();

    REP(i, 5001) {
      REP(j, 5001) { map[i][j] = 0; }
    }

    REP(i, n) {
      REP(j, n) { isUsed[i][j] = 0; }
    }

    REP(i, n) {
      int x, y;
      std::cin >> x >> y;
      map[x][y] = 1;
      pillars.push_back(P{x, y});
    }

    int res = 0;
    REP(i, n) {
      FOR(j, i + 1, n) {
        if (isUsed[i][j]) {
          continue;
        }

        isUsed[i][j] = 1;
        P a = pillars[i], b = pillars[j], v = b - a;
        int S = v.x * v.x + v.y * v.y;

        if (canCreateSite(a, b - a)) {
          res = std::max(S, res);
        } else if (canCreateSite(b, a - b)) {
          res = std::max(S, res);
        }
      }
    }

    std::cout << res << std::endl;
  }

  return 0;
}