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

#include <cmath>
#include <iostream>
using namespace std;

/*int calc(int* stations, int n, int startPoint, int count){
        int currentIndex = startPoint;
        int totalDistance = 0;
        for(int i = 0; i < count; i++){
                int forwardDist = 0, backDist = 0;
                for(int j = 1; j < n; j++){
                        if(stations[(startPoint + j) % n] == 1){
                                forwardDist = j;
                                break;
                        }
                }

                for(int j = 1; j < n; j++){
                        if(j > startPoint){
                                if(stations[n - j + startPoint] == 1){
                                        backDist = j;
                                        break;
                                }
                        }else{
                                if(stations[startPoint - j] == 1){
                                        backDist = j;
                                        break;
                                }
                        }
                }
        if(forwardDist < backDist){
                stations[(startPoint + forwardDist) % n] = 0;
                totalDistance += forwardDist;
                startPoint = (startPoint + forwardDist) % n;
        }else if(forwardDist > backDist){
                if(backDist < startPoint){
                        stations[startPoint - backDist] = 0;
                        startPoint = startPoint - backDist;
                }else{
                        stations[n - backDist + startPoint] = 0;
                        startPoint = n - backDist + startPoint;
                }
                totalDistance += backDist;
        }else{
                int fTotalDistance = totalDistance, bTotalDistance =
totalDistance; int fStations[n]; int tempStartPoint = startPoint; for(int k = 0;
k < n; k++){ fStations[k] = stations[k];
                }
                fStations[(startPoint + forwardDist) % n] = 0;
                fTotalDistance += forwardDist;
                startPoint = (tempStartPoint + forwardDist) % n;
                fTotalDistance += calc(fStations, n, startPoint, count - (i +
1));

                if(backDist < tempStartPoint){
                        stations[tempStartPoint - backDist] = 0;
                        startPoint = tempStartPoint - backDist;
                }else{
                        stations[n - backDist + tempStartPoint] = 0;
                        startPoint = n - backDist + tempStartPoint;
                }
                bTotalDistance += backDist;
                bTotalDistance += calc(stations, n, startPoint, count - (i +
1));

                if(fTotalDistance > bTotalDistance){
                        totalDistance += bTotalDistance;
                }else{
                        totalDistance += fTotalDistance;
                }
                break;
        }
        }
        return totalDistance;
}*/
int shortestDistance = 2147483647;
int getDistance(int s1, int s2, int n, int mode) {
  if (mode == 0) {
    if (s2 > s1) {
      return s2 - s1;
    } else {
      return n - s1 + s2;
    }
  } else {
    if (s2 > s1) {
      return s1 + n - s2;
    } else {
      return s1 - s2;
    }
  }
}
void calc(int *stations, int n, int startPoint, int count, int currentDist) {
  for (int i = 0; i < n; i++) {
    if (stations[(startPoint + i) % n] == 1) {
      shortestDistance = n - i;
      break;
    }
  }
  for (int i = 0; i < n; i++) {
    if (i <= startPoint) {
      if (stations[startPoint - i] == 1) {
        shortestDistance = fmin(shortestDistance, n - i);
        break;
      }
    } else {
      if (stations[n - i + startPoint]) {
        shortestDistance = fmin(shortestDistance, n - i);
        break;
      }
    }
  }
  // cout << "forward->back" << endl;
  for (int i = 1; i < n; i++) {
    if (stations[(startPoint + i) % n] == 1) {
      int tempDist = 0;
      for (int j = 1; j < n; j++) {
        if (stations[(startPoint + i + j) % n] == 1) {
          tempDist = i + getDistance((startPoint + i) % n,
                                     (startPoint + i + j) % n, n, 1);
          shortestDistance =
              shortestDistance < tempDist ? shortestDistance : tempDist;
          // cout << (startPoint + i) % n << ',' << (startPoint + i + j) % n <<
          // ',' << tempDist << endl;
          break;
        }
      }
    }
  }
  // cout << "back->forward" << endl;
  for (int i = 1; i < n; i++) {
    if (i <= startPoint) {
      if (stations[startPoint - i] == 1) {
        int tempDist = 0;
        for (int j = 1; j < n; j++) {
          if (i + j <= startPoint) {
            if (stations[startPoint - (i + j)] == 1) {
              tempDist =
                  i + getDistance(startPoint - i, startPoint - (i + j), n, 0);
              shortestDistance =
                  shortestDistance < tempDist ? shortestDistance : tempDist;
              // cout << startPoint - i << ',' << startPoint - (i + j) << ',' <<
              // tempDist << endl;
              break;
            }
          } else {
            if (stations[n - (i + j) + startPoint] == 1) {
              tempDist = i + getDistance(startPoint - i,
                                         n - (i + j) + startPoint, n, 0);
              shortestDistance =
                  shortestDistance < tempDist ? shortestDistance : tempDist;
              // cout << startPoint - i << ',' << n - (i + j) + startPoint <<
              // ',' << tempDist << endl;
              break;
            }
          }
        }
      }
    } else {
      if (stations[n - i + startPoint] == 1) {
        int tempDist = 0;
        for (int j = 1; j < n; j++) {
          if (stations[n - (i + j) + startPoint] == 1) {
            tempDist = i + getDistance(n - i + startPoint,
                                       n - (i + j) + startPoint, n, 0);
            shortestDistance =
                shortestDistance < tempDist ? shortestDistance : tempDist;
            // cout << n - i + startPoint << ',' << n - (i + j) + startPoint <<
            // ',' << tempDist << endl;
            break;
          }
        }
      }
    }
  }
}

int main() {
  int n, m, p;
  cin >> n >> m >> p;
  int stations[n];
  for (int i = 0; i < n; i++) {
    stations[i] = 0;
  }
  for (int i = 0; i < m; i++) {
    int d = 0;
    cin >> d;
    stations[d] = 1;
  }
  calc(stations, n, p, m, 0);
  cout << shortestDistance * 100 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;

/*int calc(int* stations, int n, int startPoint, int count){
        int currentIndex = startPoint;
        int totalDistance = 0;
        for(int i = 0; i < count; i++){
                int forwardDist = 0, backDist = 0;
                for(int j = 1; j < n; j++){
                        if(stations[(startPoint + j) % n] == 1){
                                forwardDist = j;
                                break;
                        }
                }

                for(int j = 1; j < n; j++){
                        if(j > startPoint){
                                if(stations[n - j + startPoint] == 1){
                                        backDist = j;
                                        break;
                                }
                        }else{
                                if(stations[startPoint - j] == 1){
                                        backDist = j;
                                        break;
                                }
                        }
                }
        if(forwardDist < backDist){
                stations[(startPoint + forwardDist) % n] = 0;
                totalDistance += forwardDist;
                startPoint = (startPoint + forwardDist) % n;
        }else if(forwardDist > backDist){
                if(backDist < startPoint){
                        stations[startPoint - backDist] = 0;
                        startPoint = startPoint - backDist;
                }else{
                        stations[n - backDist + startPoint] = 0;
                        startPoint = n - backDist + startPoint;
                }
                totalDistance += backDist;
        }else{
                int fTotalDistance = totalDistance, bTotalDistance =
totalDistance; int fStations[n]; int tempStartPoint = startPoint; for(int k = 0;
k < n; k++){ fStations[k] = stations[k];
                }
                fStations[(startPoint + forwardDist) % n] = 0;
                fTotalDistance += forwardDist;
                startPoint = (tempStartPoint + forwardDist) % n;
                fTotalDistance += calc(fStations, n, startPoint, count - (i +
1));

                if(backDist < tempStartPoint){
                        stations[tempStartPoint - backDist] = 0;
                        startPoint = tempStartPoint - backDist;
                }else{
                        stations[n - backDist + tempStartPoint] = 0;
                        startPoint = n - backDist + tempStartPoint;
                }
                bTotalDistance += backDist;
                bTotalDistance += calc(stations, n, startPoint, count - (i +
1));

                if(fTotalDistance > bTotalDistance){
                        totalDistance += bTotalDistance;
                }else{
                        totalDistance += fTotalDistance;
                }
                break;
        }
        }
        return totalDistance;
}*/
int shortestDistance = 2147483647;

void calc(int *stations, int n, int startPoint, int count, int currentDist) {
  if (count == 0) {
    // if(shortestDistance < 0){
    //	shortestDistance = currentDist;
    // }else{
    shortestDistance =
        shortestDistance < currentDist ? shortestDistance : currentDist;
    //}
  }
  if (/*shortestDistance >= 0 &&*/ currentDist > shortestDistance) {
    return;
  }
  //????????¢?´¢
  for (int i = 1; i < n; i++) {
    int index = (startPoint + i) % n;
    if (stations[index] == 1) {
      stations[index] = 0;
      currentDist += i;
      calc(stations, n, index, count - 1, currentDist);
      currentDist -= i;
      stations[index] = 1;
    }
  }
  //????????¢?´¢
  for (int i = 1; i < n; i++) {
    int index = 0;
    if (i <= startPoint) {
      index = startPoint - i;
    } else {
      index = n - i + startPoint;
    }
    if (stations[index] == 1) {
      stations[index] = 0;
      currentDist += i;
      calc(stations, n, index, count - 1, currentDist);
      currentDist -= i;
      stations[index] = 1;
    }
  }
}

int main() {
  int n, m, p;
  cin >> n >> m >> p;
  int stations[n];
  for (int i = 0; i < n; i++) {
    stations[i] = 0;
  }
  for (int i = 0; i < m; i++) {
    int d = 0;
    cin >> d;
    stations[d] = 1;
  }
  calc(stations, n, p, m, 0);
  cout << shortestDistance * 100 << endl;
  return 0;
}