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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>

#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it, X)                                                         \\
  for (__typeof((X).begin()) it = (X).begin(); it != (X).end(); it++)
#define ite iterator
#define mp make_pair
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define pb push_back
#define sec second
#define sz(x) ((int)x.size())

using namespace std;

struct timer {
  time_t start;
  timer() { start = clock(); }
  ~timer() {
    cerr << 1. * (clock() - start) / CLOCKS_PER_SEC << " secs" << endl;
  }
};

typedef istringstream iss;
typedef long long ll;
typedef pair<int, int> pi;
typedef stringstream sst;
typedef vector<int> vi;

ll dist, n, m;
ll S[6], D[6];
int decide[6];
ll ans;
ll start[6];

void f(int cur) {
  // cout<<cur<<": ";
  // rep(i,n)cout<<start[i]<<" ";cout<<endl;
  if (cur == n) {
    // rep(i,n)cout<<start[i]<<" ";
    ll res = 0;
    rep(i, n) { res = max(res, start[i] + dist * S[i]); }
    // cout<<":"<<res<<endl;
    ans = min(ans, res);
    return;
  }
  int target = decide[cur];
  rep(i, cur + 1) rep(j, i < cur ? m : 1) {
    if (i == cur) {
      if (start[target - 1] == -1)
        continue;
      start[target] = start[target - 1] + 1;
    } else {
      int car = decide[i];
      if (S[target] == S[car])
        continue;
      start[target] = start[car] + D[j] * (S[car] - S[target]);
    }
    // cout<<cur<<" start: "<<start[target]<<endl;

    if (start[target] < 0) {
      start[target] = -1;
      continue;
    }
    int ok = 1;
    int used[6] = {};
    rep(k, cur) {
      int car = decide[k];
      if (car < target && start[target] <= start[car] ||
          car > target && start[target] >= start[car]) {
        ok = 0;
        break;
      }
      ll difstart = start[car] - start[target];
      ll difS = S[target] - S[car];
      // cout<<k<<" "<<difstart<<" "<<difS<<endl;
      if (difS == 0) {
        continue;
      }
      if (difstart > 0 && difS < 0 || difstart < 0 && difS > 0) {
        continue;
      }
      if (difstart < 0 && difS < 0) {
        difstart *= -1;
        difS *= -1;
      }
      if (difstart >= difS * dist)
        continue;
      if (difstart % difS) {
        ok = 0;
        break;
      }
      ll point = difstart / difS;
      // cout<<point<<endl;
      int found = 0;
      rep(l, m) if (D[l] == point) {
        found = 1;
        if (used[l]) {
          ok = 0;
          break;
        }
        used[l] = 1;
        break;
      }
      if (!found) {
        ok = 0;
        break;
      }
    }
    if (ok) {
      f(cur + 1);
    }
    start[target] = -1;
  }
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);

  cin >> dist >> n;
  rep(i, n) cin >> S[i];
  cin >> m;
  rep(i, m) cin >> D[i];
  D[m++] = dist;
  sort(D, D + m);
  // rep(i,m)cout<<D[i]<<" ";cout<<endl;

  rep(i, n) decide[i] = i;
  memset(start, -1, sizeof(start));
  start[0] = 0;
  ans = INF * INF;
  do {
    // rep(i,n)cout<<decide[i]<<" ";cout<<endl;
    f(1);
    // cout<<endl;
  } while (next_permutation(decide + 1, decide + n));
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>

#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it, X)                                                         \\
  for (__typeof((X).begin()) it = (X).begin(); it != (X).end(); it++)
#define ite iterator
#define mp make_pair
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define pb push_back
#define sec second
#define sz(x) ((int)x.size())

using namespace std;

struct timer {
  time_t start;
  timer() { start = clock(); }
  ~timer() {
    cerr << 1. * (clock() - start) / CLOCKS_PER_SEC << " secs" << endl;
  }
};

typedef istringstream iss;
typedef long long ll;
typedef pair<int, int> pi;
typedef stringstream sst;
typedef vector<int> vi;

ll dist, n, m;
ll S[6], D[6];
int decide[6];
ll ans;
ll start[6];

void f(int cur) {
  // cout<<cur<<": ";
  // rep(i,n)cout<<start[i]<<" ";cout<<endl;
  if (cur == n) {
    // rep(i,n)cout<<start[i]<<" ";cout<<endl;
    ll res = 0;
    rep(i, n) { res = max(res, start[i] + dist * S[i]); }
    ans = min(ans, res);
    return;
  }
  int target = decide[cur];
  rep(i, cur + 1) rep(j, i < cur ? m : 1) {
    if (i == cur) {
      if (start[target - 1] == -1)
        continue;
      start[target] = start[target - 1] + 1;
    } else {
      int car = decide[i];
      if (S[target] == S[car])
        continue;
      start[target] = start[car] + D[j] * (S[car] - S[target]);
    }
    // cout<<cur<<" start: "<<start[target]<<endl;

    if (start[target] < 0) {
      start[target] = -1;
      continue;
    }
    int ok = 1;
    rep(k, cur) {
      int car = decide[k];
      if (car < target && start[target] <= start[car] ||
          car > target && start[target] >= start[car]) {
        ok = 0;
        break;
      }
      ll difstart = start[car] - start[target];
      ll difS = S[target] - S[car];
      // cout<<k<<" "<<difstart<<" "<<difS<<endl;
      if (difS == 0) {
        continue;
      }
      if (difstart > 0 && difS < 0 || difstart < 0 && difS > 0) {
        continue;
      }
      if (difstart < 0 && difS < 0) {
        difstart *= -1;
        difS *= -1;
      }
      if (difstart > difS * dist)
        continue;
      if (difstart % difS) {
        ok = 0;
        break;
      }
      ll point = difstart / difS;
      int found = 0;
      rep(l, m) if (D[l] == point) {
        found = 1;
        break;
      }
      if (!found) {
        ok = 0;
        break;
      }
    }
    if (ok) {
      f(cur + 1);
    }
    start[target] = -1;
  }
}

void main2() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);

  cin >> dist >> n;
  rep(i, n) cin >> S[i];
  cin >> m;
  rep(i, m) cin >> D[i];
  D[m++] = dist;
  sort(D, D + m);
  // rep(i,m)cout<<D[i]<<" ";cout<<endl;

  rep(i, n) decide[i] = i;
  memset(start, -1, sizeof(start));
  start[0] = 0;
  ans = INF * INF;
  do {
    // rep(i,n)cout<<decide[i]<<" ";cout<<endl;
    f(1);
    // cout<<endl;
  } while (next_permutation(decide + 1, decide + n));
  cout << ans << endl;
}

int main() { rep(i, 100000) main2(); }