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

//  0563.cpp
//  2012/11/07.

#if 1

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); }
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }

const ll INF = 1LL << 62;

ll calc(vint &v, int p) {
  ll res = 0;

  rep(i, v.size()) res += 2 * abs(p - v[i]);

  return res;
}

int main() {
  int W, H, N;
  cin >> W >> H >> N;

  vint X(N), Y(N);
  vector<pint> points(N);
  rep(i, N) {
    cin >> X[i] >> Y[i];
    points[i] = mp(X[i], Y[i]);
  }

  sort(ALL(X));
  sort(ALL(Y));

  ll best = INF, best_x = 0, best_y = 0;
  int px[] = {X[(N - 1) / 2], X[(N + 1) / 2]};
  int py[] = {Y[(N - 1) / 2], Y[(N + 1) / 2]};

  rep(i, 4) {
    int x = px[i / 2], y = py[i % 2];
    ll sub = calc(X, x) + calc(Y, y);

    int far_dst = 0;
    rep(j, N)
        chmax(far_dst, abs(x - points[j].first) + abs(y - points[j].second));

    sub -= far_dst;

    if (sub < best) {
      best = sub;
      best_x = x;
      best_y = y;
    }
  }

  cout << best << endl << best_x << " " << best_y << endl;
}

#endif

Error: TLE

Fixed submission:

//  0563.cpp
//  2012/11/07.

#if 1

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); }
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }

const ll INF = 1LL << 62;

ll calc(vint &v, int p) {
  ll res = 0;

  rep(i, v.size()) res += 2 * abs(p - v[i]);

  return res;
}

int main() {
  int W, H, N;
  cin >> W >> H >> N;

  vint X(N), Y(N);
  vector<pint> points(N);
  rep(i, N) {
    cin >> X[i] >> Y[i];
    points[i] = mp(X[i], Y[i]);
  }

  sort(ALL(X));
  sort(ALL(Y));

  ll best = INF, best_x = 0, best_y = 0;
  int mid_x = X[N / 2], mid_y = Y[N / 2];

  rep(last, N) {
    for (int x = (N - 1) / 2; x <= (N + 1) / 2; x++) {
      for (int y = (N - 1) / 2; y <= (N + 1) / 2; y++) {
        ll sub = calc(X, X[x]) + calc(Y, Y[y]);
        sub -= abs(X[x] - points[last].first);
        sub -= abs(Y[y] - points[last].second);

        if (sub < best) {
          best = sub;
          best_x = X[x];
          best_y = Y[y];
        }

        if (sub == best) {
          if (X[x] < best_x) {
            best_x = X[x];
            best_y = Y[y];
          }

          if (X[x] == best_x) {
            if (Y[y] < best_y) {
              best_y = Y[y];
            }
          }
        }
      }
    }
  }

  cout << best << endl << best_x << " " << best_y << endl;
}

#endif