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

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>

#define INFTY 1 << 28
#define INV (-1)

using namespace std;

/*
        ñªTõÖ
        ©Â©çÈ¢êA»êÉÅàß¢Å¬ÌlðÂvfðÔ·B
*/
int BinSearch(int a[], int asize, int search, bool &bFound) {
  int l = 0;
  int r = asize - 1;
  int m = 0;

  if (asize == 0) {
    bFound = false;
    return 0;
  }
  if (search < a[l]) {
    bFound = false;
    return 0;
  }
  if (a[l] == search) {
    bFound = true;
    return 0;
  }
  if (a[r] == search) {
    bFound = true;
    return r;
  }
  if (a[r] < search) {
    bFound = false;
    return r;
  }
  if (asize == 2) {
    bFound = false;
    return 0;
  }

  while (true) {
    m = (r + l) / 2;

    if (a[m - 1] < search && search < a[m]) {
      bFound = false;
      return m - 1;
    } else if (a[m] < search && search < a[m + 1]) {
      bFound = false;
      return m;
    } else if (a[m] == search) {
      bFound = true;
      return m;
    } else if (search < a[m]) {
      r = m;
    } else {
      l = m;
    }
  }
}

inline int dist(int from, int to, int D) {
  int ret = abs(to - from);
  if (ret >= (double)D / 2) {
    ret = D - ret;
  }
  return ret;
}

int main() {
  while (true) {

    int ans = 0;
    int d, n, m;
    int store[100003];

    cin >> d;

    if (d == 0)
      break;

    cin >> n >> m;

    for (int i = 0; i < n - 1; ++i) {
      cin >> store[i];
    }
    store[n - 1] = 0;
    store[n] = d;
    sort(store, store + n + 1);

    for (int i = 0; i < m; ++i) {
      int t;
      bool tb;
      cin >> t;

      int td = BinSearch(store, n + 1, t, tb);
      if (dist(store[td], t, d) == 0) {
        continue;
      } else if (td + 1 <= n) {
        ans += min(dist(store[td + 1], t, d), dist(store[td], t, d));
      } else {
        ans += dist(store[td], t, d);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdlib>
#include <iostream>
#include <vector>
#define INFTY 1 << 28
#define INV (-1)
using namespace std;

inline int dist(int from, int to, int D) {
  int ret = abs(to - from);
  if (ret >= (double)D / 2) {
    ret = D - ret;
  }
  return ret;
}

int main() {
  while (true) {

    int ans = 0;
    int d, n, m;
    vector<int> vStore;

    cin >> d;

    if (d == 0)
      break;

    cin >> n >> m;

    for (int i = 0; i < n - 1; ++i) {
      int t;
      cin >> t;
      vStore.push_back(t);
    }
    vStore.push_back(0);

    for (int i = 0; i < m; ++i) {
      int t;
      int min_d = INFTY;
      cin >> t;

      for (unsigned int j = 0; j < vStore.size(); ++j) {
        int td = dist(vStore[j], t, d);
        if (td < min_d) {
          min_d = td;
        }
      }

      ans += min_d;
    }

    cout << ans << endl;
    /*

    for(unsigned int i = 0; i < vDest.size(); ++i){
            int min_d = INFTY;
            int min_store = INV;
            vStore[min_store].second.push_back( min_d );
    }

    for(unsigned int i = 0; i < vStore.size(); ++i){
            int max_d = 0;
            for(unsigned int j = 0; j < vStore[i].second.size(); ++j){
                    ans += vStore[i].second[j];
            }
    }

    cout << ans << endl;
    */
  }
  return 0;
}