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

#include <algorithm>
#include <cstdio>
#include <deque>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, n) for (int i = 1; i <= n; i++)

int main() {
  vector<pair<int, int>> bou;
  while (1) {
    bou.clear();
    int n;
    scanf("%d", &n);
    if (n == 0)
      break;

    rep(i, n) {
      int a, b;
      scanf("%d%d", &a, &b);
      bou.push_back(make_pair(a, b));
    }
    sort(bou.begin(), bou.end());

    /*
    rep(i,bou.size()){
            printf("a %d %d\\n",bou[i].first,bou[i].second);
    }*/

    int maxi = 0;

    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        int dx = bou[i].first - bou[j].first;
        int dy = bou[i].second - bou[j].second;
        /*
        printf("%d %d (%d %d) (%d
        %d)\\n",i,j,bou[i].first,bou[i].second,bou[j].first,bou[j].second);
        printf("%2d %2d & %2d
        %2d\\n",i,j,bou[i].first+dx,bou[i].second-dy,bou[j].first+dx,bou[j].second-dy);
        printf("%2d %2d & %2d
        %2d\\n",i,j,bou[i].first-dx,bou[i].second+dy,bou[j].first-dx,bou[j].second+dy);
        */
        int an1, an2;
        if (binary_search(bou.begin(), bou.end(),
                          make_pair(bou[i].first + dy, bou[i].second - dx)) &&
            binary_search(bou.begin(), bou.end(),
                          make_pair(bou[j].first + dy, bou[j].second - dx))) {

          // if(an1==1 && an2==1){
          maxi = max(maxi, dx * dx + dy * dy);
        }
        if (binary_search(bou.begin(), bou.end(),
                          make_pair(bou[i].first - dy, bou[i].second + dx)) &&
            binary_search(bou.begin(), bou.end(),
                          make_pair(bou[j].first - dy, bou[j].second + dx))) {

          // if(an1==1 && an2==1){
          maxi = max(maxi, dx * dx + dy * dy);
        }
      }
    }

    printf("%d\\n", maxi);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <deque>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, n) for (int i = 1; i <= n; i++)

int main() {
  vector<pair<int, int>> bou;
  while (1) {
    bou.clear();
    int n;
    scanf("%d", &n);
    if (n == 0)
      break;

    rep(i, n) {
      int a, b;
      scanf("%d%d", &a, &b);
      bou.push_back(make_pair(a, b));
    }
    sort(bou.begin(), bou.end());

    /*
    rep(i,bou.size()){
            printf("a %d %d\\n",bou[i].first,bou[i].second);
    }*/

    int maxi = 0;

    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        int dx = bou[i].first - bou[j].first;
        int dy = bou[i].second - bou[j].second;
        /*
        printf("%d %d (%d %d) (%d
        %d)\\n",i,j,bou[i].first,bou[i].second,bou[j].first,bou[j].second);
        printf("%2d %2d & %2d
        %2d\\n",i,j,bou[i].first+dx,bou[i].second-dy,bou[j].first+dx,bou[j].second-dy);
        printf("%2d %2d & %2d
        %2d\\n",i,j,bou[i].first-dx,bou[i].second+dy,bou[j].first-dx,bou[j].second+dy);
        */
        int an1, an2;
        an1 = binary_search(bou.begin(), bou.end(),
                            make_pair(bou[i].first + dy, bou[i].second - dx));
        an2 = binary_search(bou.begin(), bou.end(),
                            make_pair(bou[j].first + dy, bou[j].second - dx));

        if (an1 == 1 && an2 == 1) {
          maxi = max(maxi, dx * dx + dy * dy);
        }
        an1 = binary_search(bou.begin(), bou.end(),
                            make_pair(bou[i].first - dy, bou[i].second + dx));
        an2 = binary_search(bou.begin(), bou.end(),
                            make_pair(bou[j].first - dy, bou[j].second + dx));

        if (an1 == 1 && an2 == 1) {
          maxi = max(maxi, dx * dx + dy * dy);
        }
      }
    }

    printf("%d\\n", maxi);
  }
}