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

#include <stdio.h>
#include <stdlib.h>

#define D_MAX 1000000000
#define N_MAX 100000
#define M_MAX 10000

int d;
int n;
int m;

int ad[N_MAX + 1];
int ak[M_MAX + 1];
int temp[N_MAX + 1];

void MergeSort(int array[], int left, int right) {
  int awork[N_MAX + 1];
  int mid;
  int i;
  int j;
  int x;
  int y;

  if (left >= right) {
    return;
  }

  mid = (left + right) / 2;
  MergeSort(array, left, mid);
  MergeSort(array, mid + 1, right);

  for (i = left; i <= mid; i++) {
    awork[i] = array[i];
  }

  j = right;
  for (i = mid + 1; i <= right; i++) {
    awork[i] = array[j];
    j--;
  }

  x = left;
  y = right;

  for (i = left; i <= right; i++) {
    if (awork[x] <= awork[y]) {
      array[i] = awork[x];
      x++;
    } else {
      array[i] = awork[y];
      y--;
    }
  }
}

int calcDist(int k) {
  int i;
  int diff;
  int preDiff = 0;
  int minDist = d;
  int tmpDist;
  int halfDist = d >> 1;

  for (i = 0; i <= n; i++) {
    diff = ad[i] - k;
    if (minDist > abs(diff)) {
      minDist = abs(diff);
    }
    preDiff = diff;
  }
  return minDist;
}

/*
int calcDist(int k){
        int i;
        int minDist = d;
        int tmpDist;
        int halfDist = d >> 1;

        for( i=1; i<=n; i++){
                tmpDist = abs(ad[i]-k);
                if( tmpDist > halfDist ){
                        tmpDist = d-tmpDist;
                }
                if( minDist > tmpDist ){
                        minDist = tmpDist;
                }
        }
        return minDist;
}
*/

void input(void) {
  int i;
  scanf("%d", &d);
  if (d != 0) {
    scanf("%d", &n);
    scanf("%d", &m);
    ad[0] = 0;
    for (i = 1; i <= n - 1; i++) {
      scanf("%d", &ad[i]);
    }
    ad[n] = d;
    for (i = 1; i <= m; i++) {
      scanf("%d", &ak[i]);
    }
  }
}

int main(void) {
  int i;
  int sumDist = 0;
  while (1) {
    input();
    if (d == 0) {
      break;
    }
    MergeSort(ad, 0, n);
    for (i = 1; i <= m; i++) {
      sumDist += calcDist(ak[i]);
    }
    printf("%d\\n", sumDist);
    sumDist = 0;
  }
}


Error: TLE

Fixed submission:

#include <stdio.h>
#include <stdlib.h>

#define D_MAX 1000000000
#define N_MAX 100000
#define M_MAX 10000
#define min(x, y) (((x) < (y)) ? (x) : (y))

int d;
int n;
int m;

int ad[N_MAX + 1];
int ak[M_MAX + 1];
int temp[N_MAX + 1];

void MergeSort(int array[], int left, int right) {
  int awork[N_MAX + 1];
  int mid;
  int i;
  int j;
  int x;
  int y;

  if (left >= right) {
    return;
  }

  mid = (left + right) / 2;
  MergeSort(array, left, mid);
  MergeSort(array, mid + 1, right);

  for (i = left; i <= mid; i++) {
    awork[i] = array[i];
  }

  j = right;
  for (i = mid + 1; i <= right; i++) {
    awork[i] = array[j];
    j--;
  }

  x = left;
  y = right;

  for (i = left; i <= right; i++) {
    if (awork[x] <= awork[y]) {
      array[i] = awork[x];
      x++;
    } else {
      array[i] = awork[y];
      y--;
    }
  }
}

int calcDist(int k) {
  int i;
  int j = 0;
  int sum = 0;
  int diff;
  int preDiff = 0;
  int minDist = d;
  int tmpDist;
  int halfDist = d >> 1;

  for (i = 1; i <= m; i++) {
    while (ad[j] < ak[i]) {
      j++;
    }
    minDist = min(abs(ad[j - 1] - ak[i]), abs(ad[j] - ak[i]));
    sum += minDist;
  }
  return sum;
}

void input(void) {
  int i;
  scanf("%d", &d);
  if (d != 0) {
    scanf("%d", &n);
    scanf("%d", &m);
    ad[0] = 0;
    for (i = 1; i <= n - 1; i++) {
      scanf("%d", &ad[i]);
    }
    ad[n] = d;
    for (i = 1; i <= m; i++) {
      scanf("%d", &ak[i]);
    }
  }
}

int main(void) {
  int i;
  int sumDist = 0;
  while (1) {
    input();
    if (d == 0) {
      break;
    }
    MergeSort(ad, 0, n);
    MergeSort(ak, 1, m);
    for (i = 1; i <= m; i++) {
      sumDist += calcDist(ak[i]);
    }
    printf("%d\\n", sumDist);
    sumDist = 0;
  }
}