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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rrep(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define all(a) (a).begin(), (a).end()
using namespace std;
typedef vector<int> vi;

const int base = 12 * 60 * 60;
vi memo[60][60][60];

int main() {
  vi tick(3, -1);
  rep(sec, base) {
    (tick[0] += 1) %= 60;
    if (sec % 60 == 0)
      (tick[1] += 1) %= 60;
    if (sec % 720 == 0)
      (tick[2] += 1) %= 60;

    rep(i, 60) {
      vi tmp(3);
      rep(j, 3) tmp[j] = (tick[j] + i) % 60;
      sort(all(tmp));

      memo[tmp[0]][tmp[1]][tmp[2]].push_back(sec);
    }
  }

  rep(h, 60) rep(m, 60) rep(s, 60) {
    memo[h][m][s].erase(unique(all(memo[h][m][s])), memo[h][m][s].end());
  }

  int n;
  while (cin >> n, n) {
    vi w(3);
    vector<pair<int, int>> event;
    rep(i, n) {
      cin >> w[0] >> w[1] >> w[2];
      sort(all(w));
      for (int a : memo[w[0]][w[1]][w[2]]) {
        event.push_back(make_pair(a, i));
        event.push_back(make_pair(a + base, i));
      }
    }

    sort(all(event));

    int m = event.size() / 2;
    int res_s = base, res_inv = base, nonzero = 0;
    vi cnt(n, 0);

    int l = 0, r = 0;
    while (l < m) {
      while (nonzero != n) {
        int id = event[r].second;
        if (cnt[id] == 0)
          nonzero++;
        cnt[id]++;
        r++;
      }
      while (nonzero == n) {
        int id = event[l].second;
        cnt[id]--;
        if (cnt[id] == 0)
          nonzero--;
        l++;
      }

      int inv = event[r - 1].first - event[l - 1].first;
      if (res_inv > inv) {
        res_s = event[l - 1].first;
        res_inv = inv;
      }
    }
    res_inv += res_s;
    printf("%02d:%02d:%02d %02d:%02d:%02d\\n", res_s / 3600, res_s / 60 % 60,
           res_s % 60, res_inv / 3600, res_inv / 60 % 60, res_inv % 60);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rrep(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define all(a) (a).begin(), (a).end()
using namespace std;
typedef vector<int> vi;

const int base = 12 * 60 * 60;

int main() {
  int n;
  while (cin >> n, n) {
    vector<vi> w(n, vi(3));
    rep(i, n) {
      cin >> w[i][0] >> w[i][1] >> w[i][2];
      sort(all(w[i]));
    }

    vector<vi> t(n);
    vi tick(3, -1);
    rep(sec, base) {
      (tick[0] += 1) %= 60;
      if (sec % 60 == 0)
        (tick[1] += 1) %= 60;
      if (sec % 720 == 0)
        (tick[2] += 1) %= 60;

      rep(i, 60) {
        vi tmp(3);
        rep(j, 3) tmp[j] = (tick[j] + i) % 60;
        sort(all(tmp));

        rep(j, n) {
          if (w[j] == tmp)
            t[j].push_back(sec);
        }
      }
    }

    vector<pair<int, int>> event;
    rep(i, n) {
      for (int a : t[i]) {
        event.push_back(make_pair(a, i));
        event.push_back(make_pair(a + base, i));
      }
    }
    sort(all(event));

    int m = event.size() / 2;
    int res_s = base, res_inv = base, nonzero = 0;
    vi cnt(n, 0);
    int l = 0, r = 0;

    while (l < m) {
      while (nonzero != n) {
        int id = event[r].second;
        if (cnt[id] == 0)
          nonzero++;
        cnt[id]++;
        r++;
      }
      while (nonzero == n) {
        int id = event[l].second;
        cnt[id]--;
        if (cnt[id] == 0)
          nonzero--;
        l++;
      }

      int inv = event[r - 1].first - event[l - 1].first;
      if (res_inv > inv) {
        res_s = event[l - 1].first;
        res_inv = inv;
      }
    }
    res_inv += res_s;
    printf("%02d:%02d:%02d %02d:%02d:%02d\\n", res_s / 3600, res_s / 60 % 60,
           res_s % 60, res_inv / 3600, res_inv / 60 % 60, res_inv % 60);
  }
}