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

#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef pair<int, string> P;

vector<P> anime;
vector<P> fav;

bool Overlap(int x, int y) {
  int _max = max(x, y), _min = min(x, y);
  return (_max - _min) < 30;
}

bool Check(P &p) {
  for (int i = 0; i < fav.size(); i++) {
    if (p.second != fav[i].second && Overlap(p.first, fav[i].first))
      return true;
  }
  return false;
}

int main() {
  int n;
  while (cin >> n, n) {
    anime.clear();
    fav.clear();
    for (int i = 0; i < n; i++) {
      string name;
      int weekday, start;
      cin >> name >> weekday >> start;
      start = weekday * 1440 + start / 100 * 60 + start % 100;
      if (start < 360)
        start += 1440 * 7;
      anime.push_back(P(start, name));
    }
    sort(anime.begin(), anime.end());
    int p;
    cin >> p;
    for (int i = 0; i < p; i++) {
      string name;
      cin >> name;
      for (int i = 0; i < n; i++) {
        if (anime[i].second == name) {
          fav.push_back(anime[i]);
          break;
        }
      }
    }
    bool ok = true;
    for (int i = 0; i < p && ok; i++) {
      for (int j = 0; j < p && ok; j++) {
        if (i != j && Overlap(fav[i].first, fav[j].first))
          ok = false;
      }
    }
    if (!ok) {
      cout << -1 << endl;
      continue;
    }
    anime.erase(remove_if(anime.begin(), anime.end(), Check), anime.end());
    vector<int> vst;
    int count = 0;
    for (int i = 0; i < anime.size(); i++) {
      bool ok = true;
      for (int j = 0; j < vst.size() && ok; j++) {
        if (Overlap(anime[i].first, vst[j]))
          ok = false;
      }
      if (ok) {
        count++;
        vst.push_back(anime[i].first);
      }
    }
    cout << count << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef pair<int, string> P;

vector<P> anime;
vector<P> fav;

bool Overlap(int x, int y) {
  int _max = max(x, y), _min = min(x, y);
  return (_max - _min) < 30 || (_max - _min - 1440 * 7) > -30;
}

bool Check(P &p) {
  for (int i = 0; i < fav.size(); i++) {
    if (p.second != fav[i].second && Overlap(p.first, fav[i].first))
      return true;
  }
  return false;
}

int main() {
  int n;
  fstream cin("input.txt");
  while (cin >> n, n) {
    anime.clear();
    fav.clear();
    for (int i = 0; i < n; i++) {
      string name;
      int weekday, start;
      cin >> name >> weekday >> start;
      start = (weekday * 1440 + start / 100 * 60 + start % 100) % (1440 * 7);
      anime.push_back(P(start, name));
    }
    sort(anime.begin(), anime.end());
    int p;
    cin >> p;
    for (int i = 0; i < p; i++) {
      string name;
      cin >> name;
      for (int i = 0; i < n; i++) {
        if (anime[i].second == name) {
          fav.push_back(anime[i]);
          break;
        }
      }
    }
    bool ok = true;
    for (int i = 0; i < p && ok; i++) {
      for (int j = 0; j < p && ok; j++) {
        if (i != j && Overlap(fav[i].first, fav[j].first))
          ok = false;
      }
    }
    if (!ok) {
      cout << -1 << endl;
      continue;
    }
    anime.erase(remove_if(anime.begin(), anime.end(), Check), anime.end());
    vector<int> vst;
    int count = 0;
    for (int i = 0; i < anime.size(); i++) {
      bool ok = true;
      for (int j = 0; j < vst.size() && ok; j++) {
        if (Overlap(anime[i].first, vst[j]))
          ok = false;
      }
      if (ok) {
        count++;
        vst.push_back(anime[i].first);
      }
    }
    cout << count << endl;
  }
}