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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> POINT;

int main() {
  // ifstream ifs("data.txt");

  int n;
  while (cin >> n && n) {
    vector<POINT> points;
    ll res = 0;

    // INPUT
    for (int i = 0; i < n; i++) {
      POINT pos;
      cin >> pos.first >> pos.second;
      points.push_back(pos);
    }

    sort(points.begin(), points.end());
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        POINT pos1, pos2;

        pos1.first = points[i].first - (points[j].second - points[i].second);
        pos1.second = points[i].second + (points[j].first - points[i].first);
        pos2.first = points[j].first - (points[j].second - points[i].second);
        pos2.second = points[j].second + (points[j].first - points[i].first);

        if (binary_search(points.begin(), points.end(), pos1) &&
            binary_search(points.begin(), points.end(), pos2))
          res = max((double)res, pow(pos1.first - pos2.first, 2.0) +
                                     pow(pos1.second - pos2.second, 2.0));
      }
    }
    cout << res << endl;
  }
  // cin.sync(); cin.get();
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> POINT;

int main() {
  // ifstream ifs("data.txt");

  int n;
  while (cin >> n && n) {
    vector<POINT> points;
    vector<pair<double, pair<POINT, POINT>>> direcs;

    // INPUT
    for (int i = 0; i < n; i++) {
      POINT pos;
      cin >> pos.first >> pos.second;
      points.push_back(pos);
    }

    // SET
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        direcs.push_back(
            make_pair(sqrt(pow(points[i].first - points[j].first, 2.0) +
                           pow(points[i].second - points[j].second, 2.0)),
                      make_pair(points[i], points[j])));
        if (direcs.front().second.first > direcs.front().second.second)
          swap(direcs[direcs.size() - 1].second.first,
               direcs[direcs.size() - 1].second.second);
      }
    }
    sort(direcs.begin(), direcs.end(),
         greater<pair<double, pair<POINT, POINT>>>());

    // GREEDY
    int result = 0;

    for (vector<pair<double, pair<POINT, POINT>>>::iterator it = direcs.begin();
         it != direcs.end(); it++) {
      pair<POINT, POINT> res;

      res.first.first = (*it).second.first.first - ((*it).second.second.second -
                                                    (*it).second.first.second);
      res.first.second = (*it).second.first.second +
                         ((*it).second.second.first - (*it).second.first.first);
      res.second.first =
          (*it).second.second.first -
          ((*it).second.second.second - (*it).second.first.second);
      res.second.second =
          (*it).second.second.second +
          ((*it).second.second.first - (*it).second.first.first);

      if (binary_search(it, direcs.end(), make_pair((*it).first, res),
                        greater<pair<double, pair<POINT, POINT>>>())) {
        result = (int)(pow((*it).first, 2.0) + 0.5);
        break;
      }
    }

    cout << result << endl;
  }
  // cin.sync(); cin.get();
}