Problem p00866 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define int long long
#define N 100010
using namespace std;
const int INF = 1LL << 55;
const int mod = (1e9) + 7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
template <class T> T Max(T &a, T b) { return a = max(a, b); }
template <class T> T Min(T &a, T b) { return a = min(a, b); }

vector<int> getDegs(int h, int m, int s) {
  vector<int> res({h, m, s});
  res[1] = (res[1] - res[0] + 60) % 60;
  res[2] = (res[2] - res[0] + 60) % 60;
  res[0] = 0;
  return res;
}

int toSec(int h, int m, int s) { return s + 60 * m + 60 * 60 * (h / 5); }
int toSec(vector<int> a) { return toSec(a[0], a[1], a[2]); }
bool can(int hh, int mm, int ss) { return hh % 5 == mm / 12; }

vector<vector<int>> calcSameTimes(vector<int> a) {
  vector<vector<int>> res;
  for (int h = 0; h < 60; h++)
    for (int m = 0; m < 60; m++)
      for (int s = 0; s < 60; s++) {
        if (!can(h, m, s))
          continue;
        if (toSec(a) == toSec(getDegs(h, m, s)))
          res.push_back(vector<int>({h, m, s}));
      }
  return res;
}

vector<vector<int>> calcSameTimes(int h, int m, int s) {
  vector<int> a = {h, m, s};
  sort(a.begin(), a.end());
  vector<vector<int>> res;
  do {
    vector<vector<int>> tmp = calcSameTimes(getDegs(a[0], a[1], a[2]));
    for (vector<int> t : tmp)
      res.push_back(t);
  } while (next_permutation(a.begin(), a.end()));
  sort(res.begin(), res.end());
  res.erase(unique(res.begin(), res.end()), res.end());
  return res;
}

int n;
vector<vector<vector<int>>> times;
vector<int> calcTime(int h, int m, int s) {
  vector<int> res;
  int mx = -1;
  for (int i = 0; i < n; i++) {
    vector<int> tmp;
    int mn = INF;
    for (vector<int> t : times[i]) {
      int a = toSec(h, m, s);
      int b = toSec(t[0], t[1], t[2]);
      if (b < a || mn <= b - a)
        continue;
      mn = b - a;
      tmp = t;
      break;
    }
    if (mn == INF)
      return vector<int>();
    if (mx < mn)
      mx = mn, res = tmp;
  }
  return res;
}

signed main() {
  while (1) {
    cin >> n;
    if (!n)
      break;
    times.clear();
    times.resize(n);
    for (int i = 0; i < n; i++) {
      int h, m, s;
      cin >> h >> m >> s;
      times[i] = calcSameTimes(h, m, s);
    }

    /*  for(int i=0;i<n;i++){
        cout<<"begin"<<i<<" size="<<times[i].size()<<endl;
        for(int j=0;j<times[i].size();j++)
        cout<<times[i][j][0]<<" "<<times[i][j][1]<<" "<<times[i][j][2]<<endl;
        }
    */
    vector<int> from, to;
    int mn = INF;
    for (int h = 0; h < 60; h++)
      for (int m = 0; m < 60; m++)
        for (int s = 0; s < 60; s++) {
          if (!can(h, m, s))
            continue;
          vector<int> a = {h, m, s};
          vector<int> b = calcTime(h, m, s);
          if (b.empty())
            continue;
          int as = toSec(h, m, s);
          int bs = toSec(b[0], b[1], b[2]);
          assert(bs >= as);
          if (mn > bs - as)
            mn = bs - as, from = a, to = b;
        }

    printf("%02lld:%02lld:%02lld ", from[0] / 5, from[1], from[2]);
    printf("%02lld:%02lld:%02lld\n", to[0] / 5, to[1], to[2]);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define N 100010
using namespace std;
const int INF = 1LL << 55;
const int mod = (1e9) + 7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
template <class T> T Max(T &a, T b) { return a = max(a, b); }
template <class T> T Min(T &a, T b) { return a = min(a, b); }

vector<int> getDegs(int h, int m, int s) {
  vector<int> res({h, m, s});
  res[1] = (res[1] - res[0] + 60) % 60;
  res[2] = (res[2] - res[0] + 60) % 60;
  res[0] = 0;
  return res;
}

int toSec(int h, int m, int s) { return s + 60 * m + 60 * 60 * (h / 5); }
int toSec(vector<int> a) { return toSec(a[0], a[1], a[2]); }
bool can(int hh, int mm, int ss) { return hh % 5 == mm / 12; }

vector<vector<int>> calcSameTimes(vector<int> a) {
  vector<vector<int>> res;
  for (int h = 0; h < 60; h++)
    for (int m = 0; m < 60; m++)
      for (int s = 0; s < 60; s++) {
        if (!can(h, m, s))
          continue;
        if (a == getDegs(h, m, s))
          res.push_back(vector<int>({h, m, s}));
      }
  return res;
}

vector<vector<int>> calcSameTimes(int h, int m, int s) {
  vector<int> a = {h, m, s};
  sort(a.begin(), a.end());
  vector<vector<int>> res;
  do {
    vector<vector<int>> tmp = calcSameTimes(getDegs(a[0], a[1], a[2]));
    for (vector<int> t : tmp)
      res.push_back(t);
  } while (next_permutation(a.begin(), a.end()));
  sort(res.begin(), res.end());
  res.erase(unique(res.begin(), res.end()), res.end());
  return res;
}

int n;
vector<vector<vector<int>>> times;
vector<int> calcTime(int h, int m, int s) {
  vector<int> res;
  int mx = -1;
  for (int i = 0; i < n; i++) {
    vector<int> tmp;
    int mn = INF;
    for (vector<int> t : times[i]) {
      int a = toSec(h, m, s);
      int b = toSec(t[0], t[1], t[2]);
      if (b < a || mn <= b - a)
        continue;
      mn = b - a;
      tmp = t;
      break;
    }
    if (mn == INF)
      return vector<int>();
    if (mx < mn)
      mx = mn, res = tmp;
  }
  return res;
}

signed main() {
  while (1) {
    cin >> n;
    if (!n)
      break;
    times.clear();
    times.resize(n);
    for (int i = 0; i < n; i++) {
      int h, m, s;
      cin >> h >> m >> s;
      times[i] = calcSameTimes(h, m, s);
    }

    /*  for(int i=0;i<n;i++){
        cout<<"begin"<<i<<" size="<<times[i].size()<<endl;
        for(int j=0;j<times[i].size();j++)
        cout<<times[i][j][0]<<" "<<times[i][j][1]<<" "<<times[i][j][2]<<endl;
        }
    */
    vector<int> from, to;
    int mn = INF;
    for (int h = 0; h < 60; h++)
      for (int m = 0; m < 60; m++)
        for (int s = 0; s < 60; s++) {
          if (!can(h, m, s))
            continue;
          vector<int> a = {h, m, s};
          vector<int> b = calcTime(h, m, s);
          if (b.empty())
            continue;
          int as = toSec(h, m, s);
          int bs = toSec(b[0], b[1], b[2]);
          assert(bs >= as);
          if (mn > bs - as)
            mn = bs - as, from = a, to = b;
        }

    printf("%02lld:%02lld:%02lld ", from[0] / 5, from[1], from[2]);
    printf("%02lld:%02lld:%02lld\n", to[0] / 5, to[1], to[2]);
  }
  return 0;
}