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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

#define rep(i, j) for (int i = 0; i < (j); i++)
#define INF (1LL << 60)

typedef long long ll;

ll W, H, N;
ll X[100005], Y[100005];
vector<int> sX, sY;
ll maxd;
ll sx[2], sy[2];

ll dist(ll ax, ll bx, ll ay, ll by) {
  ll ret = abs(ax - bx) + abs(ay - by);
  maxd = max(maxd, ret);
  return 2 * ret;
}

int main() {
  scanf("%lld%lld%lld", &W, &H, &N);
  rep(i, N) scanf("%lld%lld", &X[i], &Y[i]);

  rep(i, N) {
    sX.push_back(X[i]);
    sY.push_back(Y[i]);
  }
  sort(sX.begin(), sX.end());
  sort(sY.begin(), sY.end());
  //  sX.erase(unique(sX.begin(), sX.end()), sX.end());
  //  sY.erase(unique(sY.begin(), sY.end()), sY.end());

  sx[0] = sX[(sX.size() - 1) / 2];
  sx[1] = sX[(sX.size() + 1) / 2];
  sy[0] = sY[(sY.size() - 1) / 2];
  sy[1] = sY[(sY.size() + 1) / 2];
  ll res = INF, rx, ry;
  rep(i, 4) {
    ll x = sx[i / 2], y = sy[i % 2];
    //    cout << x << " " << y << endl;
    ll sum = 0;
    maxd = 0;
    rep(j, N) sum += dist(x, X[j], y, Y[j]);
    sum -= maxd;
    if (sum < res) {
      res = sum;
      rx = x;
      ry = y;
    }
  }

  /*  ll resX=W, distX=INF, resY=H, distY=INF;
  rep(k,N){
    ll mY=W, mX=H, mdX=INF, mdY=INF;
    rep(i,N){
      if(i==k) continue;
      ll dist = 0;
      rep(j,N) dist += abs(X[i]-X[j])*2;
      dist -= abs(X[i]-X[k]);
      if(dist < mdX){
        mdX = dist;
        mX = X[i];
      }
    }

    rep(i,N){
      if(i==k) continue;
      ll dist = 0;
      rep(j,N) dist += abs(Y[i]-Y[j])*2;
      dist -= abs(Y[i]-Y[k]);

      if(dist < mdY){
        mdY = dist;
        mY = Y[i];
      }
    }
    if(distX+distY > mdX+mdY){
      distX = mdX, distY = mdY;
      resY = mY, resX = mX;
    }else if(distX+distY == mdX+mdY && mX < resX){
      resY = mY;
      resX = mX;
    }else if(distX+distY == mdX+mdY && mX == resX && mY < resY){
      resY = mY;
      resX = mX;
    }
    }*/

  printf("%lld\\n%lld %lld\\n", res, rx, ry);
  //  printf("%lld\\n", distX+distY);
  //  printf("%lld %lld\\n", resX, resY);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace std;

#define rep(i, j) for (int i = 0; i < (j); i++)
#define INF (1LL << 60)

typedef long long ll;

ll W, H, N;
ll X[100005], Y[100005];

int main() {
  scanf("%lld%lld%lld", &W, &H, &N);
  rep(i, N) scanf("%lld%lld", &X[i], &Y[i]);

  ll resX = W, distX = INF, resY = H, distY = INF;
  rep(k, N) {
    ll mY = W, mX = H, mdX = INF, mdY = INF;
    rep(i, N) {
      if (i == k)
        continue;
      ll dist = 0;
      rep(j, N) dist += abs(X[i] - X[j]) * 2;
      dist -= abs(X[i] - X[k]);
      if (dist < mdX) {
        mdX = dist;
        mX = X[i];
      }
    }

    rep(i, N) {
      if (i == k)
        continue;
      ll dist = 0;
      rep(j, N) dist += abs(Y[i] - Y[j]) * 2;
      dist -= abs(Y[i] - Y[k]);

      if (dist < mdY) {
        mdY = dist;
        mY = Y[i];
      }
    }
    if (distX + distY > mdX + mdY) {
      distX = mdX, distY = mdY;
      resY = mY, resX = mX;
    } else if (distX + distY == mdX + mdY && mX < resX) {
      resY = mY;
      resX = mX;
    } else if (distX + distY == mdX + mdY && mX == resX && mY < resY) {
      resY = mY;
      resX = mX;
    }
  }

  printf("%lld\\n", distX + distY);
  printf("%lld %lld\\n", resX, resY);
  return 0;
}