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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
const int MAXN = 11111;
int dp[555][MAXN];
string name;
map<string, int> mp;
// dp[i][j] = max(dp[i - 1][j - 30]);
struct bit {
  int n, c[MAXN];
  void init(int _n) {
    n = _n;
    memset(c, 0, n * sizeof(int));
  }
  void update(int x, int y) {
    x += 2;
    while (x < n) {
      c[x] = max(c[x], y);
      x += x & -x;
    }
  }
  int get(int x) {
    x += 2;
    int ret = 0;
    while (x) {
      ret = max(ret, c[x]);
      x -= x & -x;
    }
    return ret;
  }
} b;

struct data {
  int id, time;
  bool over;
  friend bool operator<(const data &a, const data &b) {
    return a.time < b.time;
  }
} d[555];
int id[555], idx = 0;

bool notok(int a, int b) {
  if (d[a].time > d[b].time) {
    swap(a, b);
  }
  if (d[a].time + 30 > d[b].time) {
    return true;
  }
  return false;
}

int used[111112];

int main() {
  int n, m;
  string name;
  while (scanf("%d", &n) != EOF && n) {
    mp.clear();
    int week, hour;
    int maxv = 0;
    for (int i = 1; i <= n; i++) {
      cin >> name >> week >> hour;
      d[i].id = i - 1;
      d[i].over = false;
      d[i].time = week * 24 * 60 + ((hour / 100)) * 60 + ((hour % 100));
      maxv = max(maxv, d[i].time);
      // cout << d[i].time << endl;
      mp[name] = i;
    }
    scanf("%d", &m);
    getchar();
    idx = 0;
    for (int i = 0; i < m; i++) {
      getline(cin, name);
      d[mp[name]].over = true;
      id[idx++] = mp[name];
    }
    memset(used, 0, sizeof(used));
    bool die = false;
    int ans = 0;
    for (int i = 0; i < idx; i++) {
      for (int j = i + 1; j < idx; j++) {
        if (notok(id[i], id[j])) {
          die = true;
          break;
        }
      }
      // cout << id[i] << endl;
      for (int j = d[id[i]].time; j < d[id[i]].time + 30; j++) {
        used[j] = true;
      }
      ans++;
      if (die)
        break;
    }
    if (die) {
      puts("-1");
      continue;
    }
    sort(d + 1, d + n + 1);

    for (int i = 1; i <= n; i++) {
      bool flag = true;
      for (int j = d[i].time; j < d[i].time + 30; j++) {
        if (used[j]) {
          flag = false;
          break;
        }
      }
      if (flag) {
        for (int j = d[i].time; j < d[i].time + 30; j++) {
          used[j] = true;
        }
        ans++;
        // cout << i << endl;
      }
    }
    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
vector<string> to[20000];
struct A {
  int da, x;
} dp[20000];
void zy(A &a, int bx, int bda) {
  if (a.x < bx || (a.x == bx && a.da < bda))
    a.da = bda, a.x = bx;
}
set<string> st;
int main() {
  int n;
  while (cin >> n) {
    memset(dp, 0, sizeof(dp));
    int m = 15000;
    for (int i = 0; i < m; i++)
      to[i].clear();
    while (n--) {
      string s;
      cin >> s;
      int d, h, m;
      cin >> d >> h;
      m = h % 100;
      h /= 100;
      if (m >= 60)
        h++, m -= 60;
      if (h >= 24)
        h -= 24, d++;
      if (d == 7)
        d = 0;
      to[d * 1440 + h * 60 + m].push_back(s);
    }
    int P;
    cin >> P;
    st.clear();
    while (P--) {
      string s;
      cin >> s;
      st.insert(s);
    }
    for (int i = 0; i < m; i++) {
      zy(dp[i + 1], dp[i].x, dp[i].da);
      for (int q = 0; q < to[i].size(); q++) {
        if (st.find(to[i][q]) != st.end())
          zy(dp[i + 30], dp[i].x + 1, dp[i].da + 1);
        else
          zy(dp[i + 30], dp[i].x, dp[i].da + 1);
      }
    }
    // cout<<dp[m].x<<"       ";
    cout << dp[m].da << endl;
  }
}