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

#include <complex>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

#define REP(i, n, m) for (int i = n; i < m; i++)
#define rep(i, n) REP(i, 0, n)

typedef complex<double> P;

P p[3001];
char flg[5001][5001];

double cross(P a, P b) { return (a.real() * b.imag() - a.imag() * b.real()); }

double area(vector<P> v) {
  int n = v.size();
  double res = 0.0;
  rep(i, n) res += cross(v[i], v[(i + 1) % n]);
  return fabs(res / 2);
}

int main(void) {
  int n;

  while (cin >> n, n) {
    memset(flg, 0, sizeof(flg));

    rep(i, n) {
      int x, y;
      cin >> x >> y;
      p[i] = P(x, y);
      flg[x][y] = 1;
    }

    double ans = 0.0;

    // 2_Çê©ðIÔ
    rep(i, n) REP(j, i + 1, n) {
      P ab = p[i] - p[j];
      P un = (ab * P(0, +1)) / abs(ab); // abÌPÊ@üxNg
      P c = p[i] + un * abs(ab);
      P d = p[j] + un * abs(ab);

      if (c.real() - (int)c.real() != 0.0 || c.imag() - (int)c.imag() != 0.0 ||
          d.real() - (int)d.real() != 0.0 || d.imag() - (int)d.imag() != 0.0 ||
          c.real() < 0 || c.imag() < 0 || d.real() < 0 || d.imag() < 0 ||
          c.real() > 5000 || c.imag() > 5000 || d.real() > 5000 ||
          d.imag() > 5000)
        continue;

      if (flg[(int)c.real()][(int)c.imag()] &&
          flg[(int)d.real()][(int)d.imag()]) {
        vector<P> v;
        v.push_back(p[i]);
        v.push_back(p[j]);
        v.push_back(d);
        v.push_back(c);
        double cal = area(v);
        if (ans < cal)
          ans = cal;
      }
    }

    cout << (int)ans << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <complex>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

#define REP(i, n, m) for (int i = n; i < m; i++)
#define rep(i, n) REP(i, 0, n)

typedef complex<double> P;

P p[3001];
int pflg[5001][5001];

double cross(P a, P b) { return (a.real() * b.imag() - a.imag() * b.real()); }

double area(vector<P> v) {
  int n = v.size();
  double res = 0.0;
  rep(i, n) res += cross(v[i], v[(i + 1) % n]);
  return fabs(res / 2);
}

int main(void) {
  int n;

  while (cin >> n, n) {
    memset(pflg, 0, sizeof(pflg));
    rep(i, n) {
      int x, y;
      cin >> x >> y;
      p[i] = P(x, y);
      pflg[x][y] = 1;
    }

    double ans = 0.0;

    // 2_Çê©ðIÔ
    rep(i, n) REP(j, i + 1, n) {
      P ab = p[i] - p[j];
      P un = (ab * P(0, +1)) / abs(ab); // abÌPÊ@üxNg
      P c = p[i] + un * abs(ab);
      P d = p[j] + un * abs(ab);

      if (c.real() - (int)c.real() != 0.0 || c.imag() - (int)c.imag() != 0.0 ||
          d.real() - (int)d.real() != 0.0 || d.imag() - (int)d.imag() != 0.0 ||
          c.real() < 0 || c.imag() < 0 || d.real() < 0 || d.imag() < 0 ||
          c.real() > 5000 || c.imag() > 5000 || d.real() > 5000 ||
          d.imag() > 5000)
        continue;

      if (pflg[(int)c.real()][(int)c.imag()] &&
          pflg[(int)d.real()][(int)d.imag()]) {
        vector<P> v;
        v.push_back(p[i]);
        v.push_back(p[j]);
        v.push_back(d);
        v.push_back(c);
        double cal = area(v);
        if (ans < cal)
          ans = cal;
      }
    }

    cout << (int)ans << endl;
  }

  return 0;
}