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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define abs(x) ((x) > 0 ? (x) : (-(x)))
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define rst(x, k) memset(x, k, sizeof(x))
#define lowbit(x) ((x) & (-(x)))
#define h(x) (1 << (x))
#define lson (ind << 1)
#define rson (ind << 1 | 1)
#define maxn 210000
#define INF 2100001000
#define mod 1000000007LL
#define link fsafsdfas
using namespace std;
typedef long long LL;
const int maxtime = 10081;
map<string, int> bst;
string name[maxn];
int c[10082];
int ans[maxn];
int fuck[maxn];
int n, f;
void update(int pos, int v) {
  while (pos <= maxtime) {
    c[pos] += v;
    pos += lowbit(pos);
  }
}
int req(int pos) {
  int res = 0;
  while (pos > 0) {
    res += c[pos];
    pos -= lowbit(pos);
  }
  return res;
}
int cal(int a, int b) { return req(b) - req(a - 1); }
void INIT(void) {
  FOR(i, 1, n) name[i].clear();
  bst.clear();
  rst(c, 0);
  FOR(i, 1, n) {
    cin >> name[i];
    int w, t;
    cin >> w >> t;
    t -= 600;
    ans[i] = w * 1440 + (t / 100 * 60 + t % 100) + 1;
    bst[name[i]] = i;
  }
  bool ok = true;
  cin >> f;
  FOR(i, 1, f) {
    string tt;
    cin >> tt;
    int kk = bst[tt];
    int a = ans[kk];
    ans[kk] = -100;
    if (cal(a, a + 29) > 0)
      ok = false;
    update(a, 1);
    update(a + 29, 1);
  }
  if (!ok) {
    printf("-1\\n");
    return;
  }
  int cnt = 0;
  int res = f;
  FOR(i, 1, n) {
    if (ans[i] >= 0)
      fuck[cnt++] = ans[i];
  }
  sort(fuck, fuck + cnt);
  REP(i, cnt) {
    if (cal(fuck[i], fuck[i] + 29) == 0) {
      res++;
      update(fuck[i], 1);
      update(fuck[i] + 29, 1);
    }
  }
  printf("%d\\n", res);
}

int main(void) {
  while (cin >> n && n) {
    INIT();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <string>
#include <vector>
typedef std::pair<int, int> pb;

int n, m;
std::map<std::string, pb> id;

const int N = 500 + 10;
const int T = 20000;
const int ch_sz = 40;

char s[N][ch_sz];
int week[N], ti[N];

pb chose[N], check[N];
int chosesz, checksz;

char ch[ch_sz];
int d[N][T];

inline int Cal(const pb &a) {
  return a.second * 24 * 60 + a.first / 100 * 60 + a.first % 100;
}
bool rok(const pb &a, const pb &b) {
  int tota = (a.second == 0 ? 7 : a.second) * 24 * 60 + a.first / 100 * 60 +
             a.first % 100;
  int totb = (b.second == 0 ? 7 : b.second) * 24 * 60 + b.first / 100 * 60 +
             b.first % 100;
  return abs(tota - totb) < 30;
}
bool ok(const pb &a, const pb &b) {
  if (abs(Cal(a) - Cal(b)) < 30)
    return true;
  return rok(a, b);
}
bool cmp(const pb &a, const pb &b) { return Cal(a) < Cal(b); }
void work() {
  id.clear();
  checksz = chosesz = 0;

  for (int i = 0; i < n; ++i) {
    scanf("%s%d%d", s[i], &week[i], &ti[i]);
    id[s[i]] = pb(ti[i], week[i]);
  }

  scanf("%d", &m);
  for (int i = 0; i < m; ++i) {
    scanf("%s", ch);
    check[checksz++] = id[ch];
  }

  for (int i = 0; i < checksz; ++i) {
    for (int j = i + 1; j < checksz; ++j) {
      if (ok(check[i], check[j])) {
        puts("-1");
        return;
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    bool f = true;
    for (int j = 0; j < checksz; ++j) {
      if (ok(pb(ti[i], week[i]), check[j])) {
        f = false;
        break;
      }
    }
    if (f) {
      chose[chosesz++] = pb(ti[i], week[i]);
    }
  }

  std::sort(chose, chose + chosesz, cmp);
  memset(d, 0, sizeof(d));

  int mx = 0;
  if (chosesz > 0)
    mx = 1;

  for (int i = 0; i < chosesz; ++i) {
    int ed = Cal(chose[i]);
    d[i][ed + 30] = 1;
    for (int j = 0; j < i; ++j) {
      for (int k = ed; k >= 0; --k) {
        if (d[j][k] > 0 && !rok(chose[i], chose[j]))
          d[j][ed + 30] = std::max(d[j][k] + 1, d[j][ed + 30]);
        mx = std::max(mx, d[j][ed + 30]);
      }
    }
  }
  printf("%d\\n", mx + m);
}
int main() {
  while (1 == scanf("%d", &n)) {
    if (0 == n)
      break;
    work();
  }
  return 0;
}