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

#define _USE_MATH_DEFINES
#define INF 100000000
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

static const double EPS = 1e-8;

int hour2second(int h, int m, int s) { return h * 60 * 60 + m * 60 + s; }

int main() {
  int n;
  while (~scanf("%d", &n)) {
    if (n == 0)
      break;

    vector<int> departure_times;
    vector<int> arrive_times;
    for (int i = 0; i < n; i++) {
      // hh:mm:ss hh:mm:ss
      int hh[2], mm[2], ss[2];
      scanf("%d:%d:%d %d:%d:%d", hh, mm, ss, hh + 1, mm + 1, ss + 1);
      int departure_second = hour2second(hh[0], mm[0], ss[0]);
      int arrive_second = hour2second(hh[1], mm[1], ss[1]);

      departure_times.push_back(departure_second);
      arrive_times.push_back(arrive_second);
    }
    sort(departure_times.begin(), departure_times.end());
    sort(arrive_times.begin(), arrive_times.end());

    int dep_idx = 0;
    int res = 0;
    int count = 0;
    for (int arv_idx = 0; arv_idx < arrive_times.size(); arv_idx++) {
      while (dep_idx < departure_times.size()) {
        if (departure_times[dep_idx] < arrive_times[arv_idx]) {
          count++;
          dep_idx++;
          res = max(res, count);
        } else {
          count--;
          break;
        }
      }
    }
    printf("%d\\n", res);
  }
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#define INF 100000000
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

static const double EPS = 1e-8;

int hour2second(int h, int m, int s) { return h * 60 * 60 + m * 60 + s; }

class Time {
public:
  int first;
  int last;
  Time(int a, int b) : first(a), last(b) {}
  bool operator<(const Time &t1) const { return first < t1.first; }
};

int gRes = 0;

void dfs(int idx, int first, int last, const vector<Time> &times, int count) {
  gRes = max(gRes, count);
  for (int i = idx + 1; i < times.size(); i++) {
    if (times[i].first < last) {
      dfs(i, times[i].first, times[i].last, times, count + 1);
    } else {
      return;
    }
  }
}

int main() {
  int n;
  while (~scanf("%d", &n)) {
    if (n == 0)
      break;

    gRes = 1;
    vector<Time> times;
    for (int i = 0; i < n; i++) {
      // hh:mm:ss hh:mm:ss
      int hh[2], mm[2], ss[2];
      scanf("%d:%d:%d %d:%d:%d", hh, mm, ss, hh + 1, mm + 1, ss + 1);
      int first_second = hour2second(hh[0], mm[0], ss[0]);
      int last_second = hour2second(hh[1], mm[1], ss[1]);
      times.push_back(Time(first_second, last_second));
    }
    sort(times.begin(), times.end());

    dfs(0, times[0].first, times[0].last, times, 1);

    // int res = 1;
    // for(int i=0;i<times.size();){
    //   int tmp = 1;
    //   // printf("i:%d\\n",i);
    //   bool is_end = true;
    //   for(int j=i+1;j<times.size();){
    // 	 bool is_end = false;
    // 	 if(times[j].first < times[i].last){
    // 	   j++; tmp++;
    // 	   res = max(tmp,res);
    // 	   // printf("i:%d j:%d\\n",i,j);
    // 	 }
    // 	 else{
    // 	   i=j;
    // 	   break;
    // 	 }
    //   }
    //   if(is_end) i++;
    // }
    printf("%d\\n", gRes);
  }
}