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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

bool isSame(int s, int t, int u, int sec, int min, int hour) {
  vector<int> a(3), b(3);
  a[0] = s;
  a[1] = t;
  a[2] = u;
  b[0] = sec;
  b[1] = min;
  b[2] = hour;
  sort(a.begin(), a.end());

  for (int i = 0; i < 3; ++i) {
    vector<int> c = b;
    for (int j = 0; j < 3; ++j) {
      c[j] += a[i] - b[0] + 60;
      c[j] %= 60;
    }
    sort(c.begin(), c.end());
    if (a == c)
      return true;
  }
  return false;
}

int main() {
  for (;;) {
    int n;
    cin >> n;
    if (n == 0)
      return 0;

    vector<int> s(n), t(n), u(n);
    for (int i = 0; i < n; ++i)
      cin >> s[i] >> t[i] >> u[i];

    queue<pair<int, int>> q;
    vector<int> m(n, 0);
    int numZero = n;
    int ret = -1;
    int len = INT_MAX;
    for (int i = 0; i < 12 * 60 * 60; ++i) {
      int sec = i % 60;
      int min = i / 60 % 60;
      int hour = i / (60 * 12);

      for (int j = 0; j < n; ++j) {
        if (isSame(s[j], t[j], u[j], sec, min, hour)) {
          if (m[j] == 0)
            --numZero;
          ++m[j];
          q.push(make_pair(i, j));
        }
      }

      while (numZero == 0) {
        if (i - q.front().first < len) {
          len = i - q.front().first;
          ret = q.front().first;
        }
        if (m[q.front().second] == 1)
          ++numZero;
        --m[q.front().second];
        q.pop();
      }
    }

    for (int i = 0; i < 2; ++i) {
      int sec = (ret + i * len) % 60;
      int min = (ret + i * len) / 60 % 60;
      int hour = (ret + i * len) / (60 * 12);
      ostringstream oss;
      oss << setfill('0');
      oss << setw(2) << (hour / 5) << ':' << setw(2) << min << ':' << setw(2)
          << sec;
      cout << oss.str();
      if (i == 0)
        cout << ' ';
    }
    cout << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

bool isSame(int s, int t, int u, int sec, int min, int hour) {
  vector<int> a(3), b(3);
  a[0] = s;
  a[1] = t;
  a[2] = u;
  b[0] = sec;
  b[1] = min;
  b[2] = hour;
  sort(a.begin(), a.end());

  for (int i = 0; i < 60; ++i) {
    for (int j = 0; j < 3; ++j) {
      ++b[j];
      b[j] %= 60;
    }
    sort(b.begin(), b.end());
    if (a == b)
      return true;
  }
  return false;
}

int main() {
  for (;;) {
    int n;
    cin >> n;
    if (n == 0)
      return 0;

    vector<int> s(n), t(n), u(n);
    for (int i = 0; i < n; ++i)
      cin >> s[i] >> t[i] >> u[i];

    queue<pair<int, int>> q;
    vector<int> m(n, 0);
    int numZero = n;
    int ret = -1;
    int len = INT_MAX;
    for (int i = 0; i < 12 * 60 * 60; ++i) {
      int sec = i % 60;
      int min = i / 60 % 60;
      int hour = i / (60 * 12);

      for (int j = 0; j < n; ++j) {
        if (isSame(s[j], t[j], u[j], sec, min, hour)) {
          if (m[j] == 0)
            --numZero;
          ++m[j];
          q.push(make_pair(i, j));
        }
      }

      while (numZero == 0) {
        if (i - q.front().first < len) {
          len = i - q.front().first;
          ret = q.front().first;
        }
        if (m[q.front().second] == 1)
          ++numZero;
        --m[q.front().second];
        q.pop();
      }
    }

    for (int i = 0; i < 2; ++i) {
      int sec = (ret + i * len) % 60;
      int min = (ret + i * len) / 60 % 60;
      int hour = (ret + i * len) / (60 * 12);
      ostringstream oss;
      oss << setfill('0');
      oss << setw(2) << (hour / 5) << ':' << setw(2) << min << ':' << setw(2)
          << sec;
      cout << oss.str();
      if (i == 0)
        cout << ' ';
    }
    cout << endl;
  }
}